Need help removing spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bobsmith
Posts: 4
Joined: 18 Dec 2014 09:17

Need help removing spaces

#1 Post by bobsmith » 18 Dec 2014 09:35

I have a auto generated test file that had a username that combines first letter of first name with last name and part of an ID number. The problem I am having is if someone has a last name of two or more words it puts a space in that kills my automated upload. Here is part of the data:

"ACC",250123,"John","Doe","MS","jdoe123"
"ACC",250124,"Jennifer","Smith","AH","jsmith124"
"ACC",250125,"Earl","Van Little","AH","evan little125"
"ACC",250126,"Joe","Sori","MS","jsori384"
"ACC",250127,"Betsy","Homal","HU","bhomal127

I have tried writing a batch file script that removes the space in Earl's username (last field) above but the best I can do is cut off from the letter 'n' on.

Any ideas?

thanks!
Bob

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help removing spaces

#2 Post by foxidrive » 18 Dec 2014 09:38

Do you only want to change the text file, in the final term? Test this on file.txt

Code: Select all

jrepl "(.*,.*) (.*)" "$1$2" /f file.txt /o -

This uses a helper batch file called `Jrepl.bat` (by dbenham) - download from: https://www.dropbox.com/s/4otci4d4s8x5ni4/Jrepl.bat

Place `Jrepl.bat` in the same folder as the batch file or in a folder that is on the path.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Need help removing spaces

#3 Post by dbenham » 18 Dec 2014 10:20

That will remove at most one space from the username field. That may not be enough. My solution below uses a negative lookahead. It removes a space if after the space there does not exist a string of characters, followed by a quote, followed by another character.

Code: Select all

jrepl " (?!.*?\q.)" "" /x /f test.txt /o -


If you are starting with text that looks like:

Code: Select all

"ACC",250123,"John","Doe","MS"
"ACC",250124,"Jennifer","Smith","AH"
"ACC",250125,"Billy Ray","Van Little","AH"
"ACC",250126,"Joe","Sori","MS"
"ACC",250127,"Betsy","Homal","HU"

Then the following will append the correct usernames in one step:

Code: Select all

jrepl ".*?,\d*(\d{3}),\q(.).*?\q,\q(.*?)\q.*" "$0+',\x22'+($2+$3.replace(/ /g,'')+$1).toLowerCase()+'\x22'" /x /j /f test.txt /o out.txt


Dave Benham

bobsmith
Posts: 4
Joined: 18 Dec 2014 09:17

Re: Need help removing spaces

#4 Post by bobsmith » 18 Dec 2014 10:50

Thanks everyone. I really appreciate your help. The code seems to work except it added another user (and remove the space) at the end. How do I make it just replace the user? I only need the one username on the end.

Code: Select all

"ACC",250123,"John","Doe","MS","jdoe123","jdoe123"
"ACC",250124,"Jennifer","Smith","AH","jsmith124","jsmith124"
"ACC",250125,"Earl","Van Little","AH","evan little125","evanlittle125"
"ACC",250126,"Joe","Sori","MS","jsori384","jsori126"
"ACC",250127,"Betsy","Homal","HU","bhomal127,"bhomal127"

bobsmith
Posts: 4
Joined: 18 Dec 2014 09:17

Re: Need help removing spaces

#5 Post by bobsmith » 18 Dec 2014 10:54

After re-re-reading the post, I am not sure I explained the problem throughly. The text file is auto generated by our old system with the user already included in the file. I can't have it re-create the user unless I somehow strip it off the end. That's why I thought the easiest way was just to remove the space.

I guess that's why I am getting double now. Thanks again for all your help.

Bob

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Need help removing spaces

#6 Post by dbenham » 18 Dec 2014 11:14

If your source already has the username, then disregard my second solution. Just use the one liner at the top.


Dave Benham

bobsmith
Posts: 4
Joined: 18 Dec 2014 09:17

Re: Need help removing spaces

#7 Post by bobsmith » 18 Dec 2014 12:00

Thanks Dave, that worked perfectly. I appreciate all your help.

Post Reply