- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sed - insert new line within a line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2009 09:36 AM
тАО05-22-2009 09:36 AM
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
Solved! Go to Solution.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2009 09:56 AM
тАО05-22-2009 09:56 AM
SolutionHow 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.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-22-2009 02:47 PM
тАО05-22-2009 02:47 PM
Re: sed - insert new line within a line
You may be able to use sed to insert newlines:
=====================================================
sed 's:\(sqlplus -S\) / \(@.*$\):\1 /nolog\n@${HOME}/connect.sql\n\2:' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-23-2009 06:59 AM
тАО05-23-2009 06:59 AM
Re: sed - insert new line within a line
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 01:48 AM
тАО05-27-2009 01:48 AM
Re: sed - insert new line within a line
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
- Tags:
- escape sequences
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 06:03 AM
тАО05-27-2009 06:03 AM
Re: sed - insert new line within a line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2009 06:14 AM
тАО05-27-2009 06:14 AM
Re: sed - insert new line within a line
> 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...