|
|
shell programming: sed filter and \n
|
|
|
|
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status:
Offline
|
|
hi guys
I am rather new to shell programming, but have very little time left to get the grip with it!
i have a text file called logfile;
i run a sed script with the command:
cat logfile | sed -f 'cwk.sed'
the output was something like:
6 16 15 12 54 6 16 17 31 59
6 16 17 31 59 6 16 18 06 44
6 16 18 06 44 6 16 20 35 41
6 16 20 35 41 6 16 22 02 24
6 16 22 02 24 6 17 01 03 37
......
now I need to change this, so that each value would be on a separate line; something like this:
6
16
15
12
54
...
at the end of my sed script I tried to use the 's' command to substitute each space with the \n charachter, but had no luck; here's the code:
s/ /\n/g
the output i get is something like:
6n16n15n12n54n6n16n17n31n59
6n16n17n31n59n6n16n18n06n44
6n16n18n06n44n6n16n20n35n41
6n16n20n35n41n6n16n22n02n24
what am I missing?
the man page for sed says:
A line can be split by substituting a newline character into it.
To specify a newline character in the replacement string, precede
it with a backslash.
cheers!
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
"\n" isn't a newline, it's a group of characters that many languages understand should be replace with an actual new line character when parsed. Not so sed. The keyboard actually produces a real new line character when you hit the return key. What you have to do is to convince the interpreter not to treat it as an instruction to execute something. However when echoed back to the terminal it will certainly cause the text that follows it to appear on the next line. Thus, the proper substitution will invariably be a "two-line command" and thus look like the following:
sed -n 's/ /\
/gp'
Note that after typing the backslash you hit the <return> key.
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
Yuck! sed is 'orrible. That "sed", let's try and solve your problem. When substituting the newline you need to escape a *literal* newline.
That is, if "meme" contained your data you might expect something like
cat meme | sed -e "s/ /\
/g"
would globally substitute a space for a newline. But it seems that's not enough! Maybe the shell intercepts a newline? Well my shell does in any case: I had to use
cat meme | sed -e "s/ /\\
/g"
to get the effect that you're after. (Compare with perl where
perl -pe 's/ /\n/g' meme
does the same thing. I know which one I prefer!)
Cheers,
Paul
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status:
Offline
|
|
Originally posted by Paul McCann:
Yuck! sed is 'orrible. That "sed", let's try and solve your problem.
Maybe the shell intercepts a newline?
Hi Paul,
Very punny! :-} Actually the shell interpreter intercepts the backslash quote before sed sees it. A single quote will protect it from the shell, as does your pair of backslashes. However, no quotation marks are necessary if you put it in a sed script. All you need is the backslash.
|
Gary
A computer scientist is someone who, when told to "Go to Hell", sees the
"go to", rather than the destination, as harmful.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status:
Offline
|
|
Yes, yes, "intercepts a backslash" is what I *thought* had been typed (cf the doubled backslash in my working (per)version). Good to know that there are some sensible ways of doing this, and thanks for the clarification: it lines up perfectly with what the man page was trying to force through my thick skull.
Cheers,
Paul
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Aug 2003
Location: Basle, Switzerland
Status:
Offline
|
|
Here's another one using an awkward awk-onliner
awk '{for ( i = 1; i <= NF; i++ ) printf("%.2d\n", $i)}' filename.txt
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status:
Offline
|
|
yep, it worked!
sed -e "s/ /\
/g"
I'd had never thought thing of using /<return>
...also interesting this awk thing... i've got to try it out
thank you all guys!
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status:
Offline
|
|
...also interesting this awk thing... i've got to try it out
Awk is pretty cool... until you learn Perl, and then it seems kind of irrelevant. Awk is easier at first, though, and can be a good stepping stone to learning Perl (was for me).
In any case, better to know either than neither!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|