You are not logged in.

#1 2009-05-27 20:20:36

gazj
Member
From: /home/gazj -> /uk/cambs
Registered: 2007-02-09
Posts: 681
Website

Please help me with sed

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

#2 2009-05-27 20:28:01

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Please help me with sed

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

Offline

#3 2009-05-27 20:33:30

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Please help me with sed

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

#4 2009-05-27 20:36:50

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Please help me with sed

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

#5 2009-05-27 20:39:29

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Please help me with sed

Procyon: Good call. To clarify, whitespace (such as spaces, tabs, etc.) separates shell args, and echo separates each argument with spaces.

Offline

#6 2009-05-27 20:47:55

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Please help me with sed

learn somethin new everyday...

Offline

#7 2009-05-27 20:51:00

gazj
Member
From: /home/gazj -> /uk/cambs
Registered: 2007-02-09
Posts: 681
Website

Re: Please help me with sed

I changed my script with your sed command Peasantoid, Thank you very much smile

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 smile

Thanks Guys

Offline

#8 2009-05-27 21:26:38

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Please help me with sed

gazj wrote:

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.

Procyon wrote:

--> 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

#9 2009-05-27 22:08:27

gazj
Member
From: /home/gazj -> /uk/cambs
Registered: 2007-02-09
Posts: 681
Website

Re: Please help me with sed

yes in c64 basic

print out$

would produce the value of out$

but

print "out$"

would print exactly what is inside the quotes in this case out$,  I would have expected similar with bash, but this was not the case smile

Offline

#10 2009-05-27 22:23:04

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Please help me with sed

Ah, the wonderful world of quoting rules :-)

Offline

#11 2009-05-27 23:14:49

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Please help me with sed

You don't need the cat either btw...

grep -i $PRS $DATFILE

would do what you need wink


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

Board footer

Powered by FluxBB