insert empty lines with sed (shell script)

insert empty lines with sed (shell script)

Post by Harald Stede » Fri, 31 Oct 2003 19:40:51



Hello newsgroup,

I would like to insert empty lines into a textfile via sed.
What I have is the position of the line as a shell variable.
As I tried several ways to implement this code into my script and did
not succed, I ask for help here.
It should look somehow like the following lines, but I do not know how
to tell sed, the line where it should insert an emty line.

linenumber=`cat $file | egrep -o -o "BLATT   2" sed -e's/:.*//g'`
sed -e '($linenumber)i\          # the problem is in this line *
\
' < oldfile > newfile

*the brackets are just to show my intention.

Any suggestions?

Thanks in advance,

Harald Steden

 
 
 

insert empty lines with sed (shell script)

Post by Andreas Kahar » Fri, 31 Oct 2003 20:09:24



> Hello newsgroup,

> I would like to insert empty lines into a textfile via sed.
> What I have is the position of the line as a shell variable.
> As I tried several ways to implement this code into my script and did
> not succed, I ask for help here.
> It should look somehow like the following lines, but I do not know how
> to tell sed, the line where it should insert an emty line.

> linenumber=`cat $file | egrep -o -o "BLATT   2" sed -e's/:.*//g'`
> sed -e '($linenumber)i\          # the problem is in this line *
> \
> ' < oldfile > newfile

The single quoted sed command won't be interpolated by the
shell.  Use double quotes instead:

sed "$linenumber"G oldfile >newfile

(appends a newline followed by the contents of the hold space
(which is empty) after the specified line).

--
Andreas K?h?ri

 
 
 

insert empty lines with sed (shell script)

Post by Harald Stede » Fri, 31 Oct 2003 20:17:54


That is the solution! Thanks!

Harald Steden

Quote:

> The single quoted sed command won't be interpolated by the
> shell.  Use double quotes instead:

> sed "$linenumber"G oldfile >newfile

> (appends a newline followed by the contents of the hold space
> (which is empty) after the specified line).

 
 
 

insert empty lines with sed (shell script)

Post by William Par » Sat, 01 Nov 2003 05:11:19



> Hello newsgroup,

> I would like to insert empty lines into a textfile via sed.
> What I have is the position of the line as a shell variable.
> As I tried several ways to implement this code into my script and did
> not succed, I ask for help here.
> It should look somehow like the following lines, but I do not know how
> to tell sed, the line where it should insert an emty line.

> linenumber=`cat $file | egrep -o -o "BLATT   2" sed -e's/:.*//g'`
> sed -e '($linenumber)i\          # the problem is in this line *
> \
> ' < oldfile > newfile

> *the brackets are just to show my intention.

Something like this ?

    cp oldfile newfile
    echo "200a

    .
    100a

    .
    w" | ed newfile

Just make sure the line numbers are bottoms up. :-)

--

Linux solution for data management and processing.

 
 
 

insert empty lines with sed (shell script)

Post by rakesh shar » Sat, 01 Nov 2003 06:56:27



Quote:

> I would like to insert empty lines into a textfile via sed.
> What I have is the position of the line as a shell variable.
> As I tried several ways to implement this code into my script and did
> not succed, I ask for help here.
> It should look somehow like the following lines, but I do not know how
> to tell sed, the line where it should insert an emty line.

> linenumber=`cat $file | egrep -o -o "BLATT   2" sed -e's/:.*//g'`
> sed -e '($linenumber)i\          # the problem is in this line *
> \
> ' < oldfile > newfile

> *the brackets are just to show my intention.

you are passing the $linenumber variable inside '...', and
shell variables within single quotes are not interpolated.
for that we need double quotes.

sed -e "${linenumber}i\\
\\
" < oldfile > newfile

note: using \\ as "..." would interpret it to \ which is what sed needs.

or we can do this if we want to avoid using an escaped newline:

NL=$'\n'
sed -e "${linenumber}i\\${NL}" < oldfile > newfile