I have file like this
FileA:
abc , "helloworld" , america
def,asia, japan
ghi, africa, ipl
Output Needed:
abc,"helloworld",america
def,asia,japan
ghi,africa,ipl
I would like to implement using awk.
I want to trim each field for its leading and trailing spaces.
for the past 6 posts, you have been asking awk questions, so what have you tried so far? have you been reading up on awk ?
Hi ..
USE THIS Command
awk '{gsub(" ","",$0); print $0;}' filename
regards
Manish
The above script just removes spaces from any where in the line.
I like to trim each field in a comma seperated file.
I wrote the following code:
awk '{gsub("^[ ]*","",$1);gsub("^[ ]*","",$2) ;gsub("^[ ]*","",$3) print $1,$2,$3;}' filename > filename1
awk '{gsub("[ ]*$","",$1);gsub("[ ]*$","",$2) ;gsub("^[ ]*$","",$3) print $1,$2,$3;}'filename1 > filename
The problem with this code is it not versatile for different files with different number of fields.
Appreciate help on making this versatile
This removes the spaces except the spaces within the double quotes:
awk -F"\"" '{for(i=1;i<=NF;i++){if(i%2)gsub(" ","",$i)}}1' OFS="\"" file
Regards
Thankyou Franklin52
This was what i was looking for that does triming and not with in double quotes.
Thanks again
nawk -F"\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("^[ ]*","",$i);gsub("[ ]*$","",$i)}}1' OFS="\"" filetst1
Modified as the earlier version was eating away the spaces between words in a csv thanks!!
Sorry the above was not doing trimming properly.
This works fine
nawk -F"|" '{for(i=1;i<=NF;i++){gsub("^[ ]*","",$i);gsub("[ ]*$","",$i)}}1' OFS="|" filetst1