You are not logged in.
Pages: 1
I need sed to rearange a csv file, I need to change the a comma into a tab followed by a pipe symbol. Then pass the outputing string through echo. I only get a space instead of a tab, anyone know why?
soutput=`cat $DATFILE | grep -i $PRS | cut -d ',' -f 1-5 | sed 's/,/\t|/g'`
Offline
this worked for me, should get you going... 's///g' means literal, for \t to expand tab you need "s///g" (double quotes).
┌─[ 16:20 ][ blue:~ ]
└─> echo 'one,two,three,four' > temp
┌─[ 16:26 ][ blue:~ ]
└─> sed -i "s/,/\t|/g" temp
┌─[ 16:26 ][ blue:~ ]
└─> cat temp
one |two |three |four
//github/
Offline
My experience is that \t isn't expanded in double quotes (shell: bash). This should work, though:
soutput=`cat $DATFILE | grep -i $PRS | cut -d ',' -f 1-5 | sed $'s/,/\t|/g'`
Note the dollar sign in front of the sed substitution string.
edit: That's odd, never mind. It seems that sed expands \t on its own.
Last edited by Peasantoid (2009-05-27 20:35:31)
Offline
I don't think the problem is \t expansion (it should work with ", ', or \\t), but how you check whether the tabs are really there:
--> echo 'a,b,c' | sed 's/,/\t|/g'
a |b |c
--> out=$(echo 'a,b,c' | sed 's/,/\t|/g')
--> echo $out
a |b |c
--> echo "$out"
a |b |c
Offline
Procyon: Good call. To clarify, whitespace (such as spaces, tabs, etc.) separates shell args, and echo separates each argument with spaces.
Offline
I changed my script with your sed command Peasantoid, Thank you very much
Still was only getting one space, so I also done as you said Procyon and it worked
Going on the idea of c64 basic (am showing the only means of programming I am good at and showing my age)
echo "$out" (or print "$out" in basic)
I would expect to get
$out
I thought the -e switch in echo enabled \t and friends.
Either way it works and I have learn't something new
Thanks Guys
Offline
echo "$out" (or print "$out" in basic)
I would expect to get
$out
First you set 'out' to mean $sth, and than you call it.
--> out=$(echo 'a,b,c' | sed 's/,/\t|/g')
--> echo $out
I don't know c64 basic, but I've heard that QBasic is a great language - Windows was written in QBasic ;-)
Offline
Offline
Ah, the wonderful world of quoting rules :-)
Offline
You don't need the cat either btw...
grep -i $PRS $DATFILE
would do what you need
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
Pages: 1