linefeeds in sed 
Author Message
 linefeeds in sed

Well, there is no comp.lang.sed, so I'll try awk'ers with my sed question:

I'd like to insert a blank line whenever a line begins with a certain
string, say "ABC". For example, input is:

DEF 456
ABC 789
GHI 135
ABC 123
XYZ 999

Output would be:

DEF 456

ABC 789
GHI 135

ABC 123
XYZ 999

I'd like to use sed to do this, in a Bourne script. This is what I tried
initially:

#!/bin/sh
sed 's/ABC/\nABC/' <<EOF
DEF 456
ABC 789
GHI 135
ABC 123
XYZ 999
EOF

But this doesn't work .. it only inserts an 'n' in front of all ABC's. Other
flavors I tried include:

sed 's/ABC/\\nABC/'
sed 's/ABC/\\\nABC/'
sed "s/ABC/\nABC/"
sed "s/ABC/\\nABC/"
sed "s/ABC/\\\nABC/"

None of these work either.

I even tried the following, based on something I found at 'sed 1 liners'
( http://www.*-*-*.com/ ):

#!/usr/bin/bash
sed 's/abc'"/`echo \\\rABC`/" <<EOF
abc
abc
EOF

But this also fails.

So, isn't there any way to embed a newline (or any control character, for
that matter) in the OUTPUT of sed?

Thanks,
-Bob
 Andover, MA



Sat, 07 Jun 2003 22:35:55 GMT  
 linefeeds in sed
I just found one solution, and it is deceptively simple. I post it here for
any other interested parties. If anyone else has other solutions, pls let me
know.

My solution consists simply of entering a newline - LITERALLY - in the
script, preceeding that newline with a '\'.

For example:

#/bin/sh
sed 's/ABC/\
ABC/' <<EOF
DEF 456
ABC 789
GHI 135
ABC 123
XYZ 999
EOF

That's all there is to it!

Found it from a redhat discusion forum archive:

http://faqchest.dynhost.com/linux/REDHAT/redhat-97/redhat-9708/redhat...


Quote:
> Well, there is no comp.lang.sed, so I'll try awk'ers with my sed question:

> I'd like to insert a blank line whenever a line begins with a certain
> string, say "ABC". For example, input is:

> DEF 456
> ABC 789
> GHI 135
> ABC 123
> XYZ 999

> Output would be:

> DEF 456

> ABC 789
> GHI 135

> ABC 123
> XYZ 999

> I'd like to use sed to do this, in a Bourne script. This is what I tried
> initially:

> #!/bin/sh
> sed 's/ABC/\nABC/' <<EOF
> DEF 456
> ABC 789
> GHI 135
> ABC 123
> XYZ 999
> EOF

> But this doesn't work .. it only inserts an 'n' in front of all ABC's.
Other
> flavors I tried include:

> sed 's/ABC/\\nABC/'
> sed 's/ABC/\\\nABC/'
> sed "s/ABC/\nABC/"
> sed "s/ABC/\\nABC/"
> sed "s/ABC/\\\nABC/"

> None of these work either.

> I even tried the following, based on something I found at 'sed 1 liners'
> (http://www.cornerstonemag.com/sed/sed1line.txt):

> #!/usr/bin/bash
> sed 's/abc'"/`echo \\\rABC`/" <<EOF
> abc
> abc
> EOF

> But this also fails.

> So, isn't there any way to embed a newline (or any control character, for
> that matter) in the OUTPUT of sed?

> Thanks,
> -Bob
>  Andover, MA



Sat, 07 Jun 2003 22:52:14 GMT  
 linefeeds in sed

Quote:

> I just found one solution, and it is deceptively simple.

Not simple enough.

Quote:
> I post it here for any other interested parties. If anyone else
> has other solutions, pls let me know.

Sed has a command for inserting text. It's the i (insert) command.
In fact, sed has lots of commands that aren't the s (substitute)
command. But because s seems to be the sharpest tool in the sed
drawer, it's the one people tend to grab first, even when the
problem has nothing whatever to do with substitution, as in this
case.

It may surprise you to learn that there are as many different sed
commands as there are letters in the alphabet:

    1  a       10  i       19  t
    2  b       11  l       20  w
    3  c       12  n       21  x
    4  d       13  N       22  y
    5  D       14  p       23  {
    6  g       15  P       24  :
    7  G       16  q       25  #
    8  h       17  r       26  =
    9  H       18  s

Quote:
> My solution consists simply of entering a newline - LITERALLY - in the
> script, preceeding that newline with a '\'.

> For example:

> #/bin/sh
> sed 's/ABC/\
> ABC/' <<EOF
> DEF 456
> ABC 789
> GHI 135
> ABC 123
> XYZ 999
> EOF

> That's all there is to it!

Overkill! :-)

$ sed '/^ABC/i\

Quote:
> ' <<EOF
> DEF 456
> ABC 789
> GHI 135
> ABC 123
> XYZ 999
> EOF

DEF 456

ABC 789
GHI 135

ABC 123
XYZ 999
$

Look, Ma! No needless substitution!

--
Jim Monty

Tempe, Arizona USA



Sun, 08 Jun 2003 02:47:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. replacing ^M characters with linefeed

2. Removal of a linefeed in

3. C3.1 Dos Printer LINEFEED Problem

4. Document editor that strips linefeeds?

5. Binary output to stdout without linefeeds, possible?

6. How do I parse with respect to a Linefeed character

7. RDE linefeeds

8. Carriage Return without a linefeed on Win95?

9. Linefeed in Unix files

10. Linefeed Character

11. Newline (linefeed) suppress.

12. No Linefeed on Text Screen using Backspace?

 

 
Powered by phpBB® Forum Software