Willybaste
Willybaste

Reputation: 3

Add a line after an occurrence

The variables are

espejo=u456789
usuario=u123456
grupo=unixgr

I want to add after the line occurrence of variable $espejo, the text in variables $usuario and $grupo:

cp $DIRECTORY/usu.txt  $DIRECTORY/usu.txt.`date '+%F'`
sed "/${espejo}/a\ ${usuario}                ${grupo}" $DIRECTORY/usu.txt.`date '+%F'` > $DIRECTORY/usu.txt

I got this error during the execution:

sed: 0602-404 Function /u456789/a\u123456 unixgr cannot be parsed.

I don't know what is wrong.

Upvotes: 0

Views: 60

Answers (3)

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed "/$espejo/r /dev/stdin" file  <<<" ${usuario}                ${grupo}"

Upvotes: 0

Fazlin
Fazlin

Reputation: 2337

Try this:

sed "s:${espejo}:${espejo}\n${usuario}                ${grupo}:" $DIRECTORIO/usu.txt.`date '+%F'` > $DIRECTORIO/usu.txt

If the above just adds n instead of newline, try this:

sed "s:${espejo}:${espejo}\
${usuario}                ${grupo}:" $DIRECTORIO/usu.txt.`date '+%F'` > $DIRECTORIO/usu.txt

This will append the new variable in new line. I guess you are facing the problem since ${espejo} contains / character in it.

Upvotes: 0

Slava Semushin
Slava Semushin

Reputation: 15204

Try the following:

sed "/${espejo}/a\
${usuario} ${grupo}" $DIRECTORIO/usu.txt.`date '+%F'` > $DIRECTORIO/usu.txt

Note that after the backslash on first line there is no any other symbols except new line.

Upvotes: 1

Related Questions