1834207 Members
2521 Online
110066 Solutions
New Discussion

Formatting File

 
SOLVED
Go to solution
Sanjay Tailor
Frequent Advisor

Formatting File

Hello,

I have an ASCII flat file which has a lot of white space ( spaces ) in between the fields. The field separators are quoted comma ( "," ). How can I remove these spaces between the field separators using something like sed, awk or tr?
A specific example or answer would be nice.

Thanks,
Sanjay.
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: Formatting File

Sanjay:

You can "squeeze" multiple blanks down to one with 'tr'. For example"

# echo "xxx yyy zzz"|tr -s " " " "

yields "xxx yyy zzz"

...JRF...
Tracey
Trusted Contributor

Re: Formatting File

I use the following to remove the tilde ~ sign at the end of each ascii row:

sed "s/~$//p" INFILE > OUTFILE
Leslie Chaim
Regular Advisor

Re: Formatting File

With tr and the -s option the second argument is not needed.

echo "aaa bbb ccc" yeilds
aaa bbb ccc
echo "aaa bbb ccc" | tr -s " " yeilds
aaa bbb ccc
If life serves you lemons, make lemonade
Vikas Khator
Honored Contributor

Re: Formatting File

Hi ,

Can you attach a small file so that we can get you the right solution.
Keep it simple
Anthony deRito
Respected Contributor
Solution

Re: Formatting File

Sanjay, so you have a file called FILE1 with strings that look like this:

field1 "," field2 "," field3 "," filed4

You can use the command:

sed 's/ //g' FILE1

to yeild the output:

field1","field2","field3","filed4","

Tony


Sanjay Tailor
Frequent Advisor

Re: Formatting File

Hello Anthony,

With the suggestion from James, I got the fileds down form multiple to single spaces. I tried what you suggested using sed and it did work. Yet one of my fields is a title ( example "The Calculus Book." When I ran sed it got rid of all the spaces including the one in the title. Like I said my field separators are "," My spaces are on either sides of these. Can write something in sed to just take out the spaces here?

Thanks,

Sanjay.
Alan Riggs
Honored Contributor

Re: Formatting File

try:

sed 's/[ ]*,[ ]*/ , /g' t
Alan Riggs
Honored Contributor

Re: Formatting File

Or:

sed 's/[ ]*,[ ]*/,/g' t

if you want no spaces at all around the commas
Leslie Chaim
Regular Advisor

Re: Formatting File

Sanjay,

Try this

sed -e 's/ *(",") */1/g' inpfile > outfile
If life serves you lemons, make lemonade
Anthony deRito
Respected Contributor

Re: Formatting File

Hmmm, I see the problem. You would not want the spaces to go away in the title of the book. Anyone else...

Tony
Sanjay Tailor
Frequent Advisor

Re: Formatting File

Hello All,

I tried what Anthony suggested and got the spaces removed everywhere. I then tried:
sed 's/ "," /","/g' file1 > file2

And it worked!!! This seems to be the format I am looking for.

Thank you.
Sanjay.