Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > shell programming: sed filter and \n

shell programming: sed filter and \n
Thread Tools
wise owl
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status: Offline
Reply With Quote
Aug 17, 2003, 05:07 PM
 
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!
     
Gary Kerbaugh
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Aug 17, 2003, 09:31 PM
 
   "\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.
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Aug 17, 2003, 09:31 PM
 
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
     
Gary Kerbaugh
Dedicated MacNNer
Join Date: Jul 2001
Location: NC
Status: Offline
Reply With Quote
Aug 17, 2003, 09:49 PM
 
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,
&nbsp;&nbsp;&nbsp;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.
     
Paul McCann
Mac Enthusiast
Join Date: Nov 2001
Location: Adelaide, South Australia
Status: Offline
Reply With Quote
Aug 17, 2003, 10:58 PM
 
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
     
Burana
Fresh-Faced Recruit
Join Date: Aug 2003
Location: Basle, Switzerland
Status: Offline
Reply With Quote
Aug 18, 2003, 04:39 AM
 
Here's another one using an awkward awk-onliner

awk '{for ( i = 1; i <= NF; i++ ) printf("%.2d\n", $i)}' filename.txt
     
wise owl  (op)
Fresh-Faced Recruit
Join Date: Aug 2003
Location: London
Status: Offline
Reply With Quote
Aug 18, 2003, 09:32 AM
 
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!
     
neilw
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Aug 18, 2003, 10:32 PM
 
...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!
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:18 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,