Operating System - HP-UX
1752856 Members
3656 Online
108790 Solutions
New Discussion юеВ

Re: sed - insert new line within a line

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

sed - insert new line within a line

Hello all,
I have an interesting problem that I have not been able to solve even after seaching the forums.

I'm trying to modify many many scripts so that
every line like:
sqlplus -S / @
becomes:
sqlplus -S /nolog
@${HOME}/connect.sql
@

Any suggestions?

RayB
6 REPLIES 6
Hein van den Heuvel
Honored Contributor
Solution

Re: sed - insert new line within a line


How about using PERL for the job?

With explicit old and new files:
# perl -pe 's%/ @%/nolog\n@\${HOME}/connect.sql\n@% if /^sqlplus/' old > new


or add -i for 'in place':

# perl -i -pe 's%/ @%/nolog\n@\${HOME}/connect.sql\n@% if /^sqlplus/' file

The $ in ${HOME} has the be escaped.
and I uise % instead of the more traditional / as match delimiter due to the / being in the strings.

The 'if /^sqlplus/' insists on sqplus being the first word, no whitepace, on a line. That can be loosened up if need be.


hth,
Hein.



Dennis Handly
Acclaimed Contributor

Re: sed - insert new line within a line

You may want to use something easier to use like awk or perl.

You may be able to use sed to insert newlines:
=====================================================
sed 's:\(sqlplus -S\) / \(@.*$\):\1 /nolog\n@${HOME}/connect.sql\n\2:' file
James R. Ferguson
Acclaimed Contributor

Re: sed - insert new line within a line

Hi Ray:

Dennis's solution (using HP's standard 'sed') doesn't actually create the separation of lines desired. However, this is easily rectified by doing:

# cat ./myrepl
#!/usr/bin/sed -f
s:\(sqlplus -S\) / \(@.*$\):\1 /nolog\
@${HOME}/connect.sql\
\2:

If you are using GNU 'sed' you should be able to use his original one-liner directly; _AND_ more importantly, you could use an '--in-place' or '-i' switch like Perl's '-i' to obviate writing the output to a temporary file and then moving the temporary file over (in place of) the unmodified original file.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: sed - insert new line within a line

>JRF: (using HP's standard sed) doesn't actually create the separation of lines desired.

It appears that sed doesn't recognize escape chars except for very limited positions. It only understands backslashes for regexp and for \n in command addresses and at the end of a line.

It also understands \n in the y command:
sed -e 's:\(sqlplus -S\) / \(@.*$\):\1 /nolog^A@${HOME}/connect.sql^A\2:' \
-e 'y/^A/\n/' file

(I've entered control A in the text.)

And from JRF's suggestion, you can do it by this "one" liner:
sed -e 's:\(sqlplus -S\) / \(@.*$\):\1 /nolog\
@${HOME}/connect.sql\
\2:' file

Other old threads about lack of escape sequences:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1123925
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=347712
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=9025
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=89288
Raynald Boucher
Super Advisor

Re: sed - insert new line within a line

Hello gents,
I used a combination of JR's and Dennis' solution and it works fine.

I tried Hein's method with perl but could not get it to work when the string to be matched was at the end of the line.

Is there a good (simple to understand) book out there that explains the use of those special characters whithin sed and perl?

I sure could use it.

Thanks again,

RayB
James R. Ferguson
Acclaimed Contributor

Re: sed - insert new line within a line

Hi (again) Ray:

> I tried Hein's method with perl but could not get it to work when the string to be matched was at the end of the line.

As Hein noted, you might want to drop the caret ('^') from the 'if /^sqlplus/' part. A caret anchors a match to the beginning of a line. Hence, in this case, any leading whitespace voids the match.

> Is there a good (simple to understand) book out there that explains the use of those special characters whithin sed and perl?

The 'regexp(5)' manpages enumerate the various metacharacters:

http://docs.hp.com/en/B2355-60130/regexp.5.html

Regards!

...JRF...