How to remove space from each record in file?

Hi ,
I want to remove space from each record in one file My file is like

BUD, BDL 
ABC, DDD, ABC
ABC, DDD, DDD, KKK

The o/p should be

BUD,BDL 
ABC,DDD,ABC
ABC,DDD,DDD,KKK

Can any one help me regarding this?

If you want to remove space anywhere in the file.

sed 's/[ ]//g' file
1 Like

Did you mean, to do in a shell script?

sed 's/ //g' MyFile

cat temp |sed 's/, /,/g'>newtemp

tr -d ' ' < infile > outfile

@kg_gaurav, would you elaborate on the significance of using cat in your pipeline?

i considered all data like
BUD, BDL
ABC, DDD, ABC
ABC, DDD, DDD, KKK

are in file temp. so cat that and pipped with sed. well i usually use pipe you may directely use

sed 's/, /,/g' temp >newfile
awk '{ gsub(/,[ ]/,",",$0); print }' file
perl -i -pe 's/ //g' input.txt
$ awk '{gsub(" ","")}1' input.txt
BUD,BDL
ABC,DDD,ABC
ABC,DDD,DDD,KKK

Another approach:

awk -F", " '$1=$1' OFS=, file
awk '$1=$1' OFS= file

Franklin's and busyboy's solution might be preferable since they would leave space in the records itself in tact.

Combined with multiple spaces:

awk -F',[ \t]*' '{$1=$1}1' OFS=, file
sed 's/,[[:blank:]]*/,/g' file
1 Like