sed works with echo but not working with cat

June 20, 2011 at 00:06:21
Specs: solaris
#echo "A B C D " | sed 's/[ ][ ]*/,/g'

output:
A,B,C,D,

But

#cat err.txt | sed 's/[ ][ ]*/,/g'

output:
COUNTRY INT.D Area Population land.line cell.no

Afghanistan .af 647500 28513677 33100 12000
Albania .al 28748 3544808 255000 1100000
Algeria .dz 2381740 32129324 2199600 1447310
Andorra .ad 468 69865 35000 23500
Angola .ao 1246700 10978552 96300 130000

#cat err.txt

COUNTRY INT.D Area Population land.line cell.no

Afghanistan .af 647500 28513677 33100 12000
Albania .al 28748 3544808 255000 1100000
Algeria .dz 2381740 32129324 2199600 1447310
Andorra .ad 468 69865 35000 23500
Angola .ao 1246700 10978552 96300 130000


when sed is getting input from echo it replaces all blank spaces with ","
but when sed gets its input from cat it does nothing.
I want to replace all blank spaces in a file with ","
I am using solaris


See More: sed works with echo but not working with cat

Report •


#1
June 20, 2011 at 07:15:23
The correct syntax to read a file with sed is:

sed 's/[ ][ ]*/,/g' err.txt



Report •

#2
June 20, 2011 at 21:17:17
thx nails for ur reply

I earlier tried sed 's/[ ][ ]*/,/g' err.txt also but sed did nothing and printed the file without any change

vikram@nmsdev2: sed 's/[ ][ ]*/,/g' err.txt
COUNTRY INT.D Area Population land.line cell.no

Afghanistan .af 647500 28513677 33100 12000
Albania .al 28748 3544808 255000 1100000
Algeria .dz 2381740 32129324 2199600 1447310
Andorra .ad 468 69865 35000 23500
Angola .ao 1246700 10978552 96300 130000



Report •

#3
June 21, 2011 at 09:09:22
I am running this sed command on my Solaris 9 box with no problems:

sed 's/[ ][ ]*/,/g' err.txt

Perhaps you are running an old version of Solaris?

If you have the /usr/xpg4/bin directory, try the version of sed if it's there:

/usr/xpg4/bin/sed 's/[ ][ ]*/,/g' err.txt

Finally, you can try this:

sed -e 's/[ ]/,/g'

Of course, if you have more than 1 space in a row, each space is replaced by a comma.



Report •

Related Solutions

#4
June 21, 2011 at 21:44:59
I am using solaris 10
I think my sed version is creating problem

thx nails for ur efforts and support


Report •


Ask Question