- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: A few sed questions
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
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
тАО08-07-2008 03:00 AM
тАО08-07-2008 03:00 AM
Re: A few sed questions
> Dennis: I would suggest you not use sed for something this complicated. awk is much easier to understand.
While 'sed' was not my prefereence either, I would use Perl for the second solution:
# perl -ple 'if (/^install/../^endif/) {print "newline ADDED" if /^endif/}' file
...short, sweet and can be inlined in a shell script.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 12:37 PM
тАО08-13-2008 12:37 PM
Re: A few sed questions
input
*************************
instal: a b c
ifeq
whatever
endif
********************
I want to do string substition on "install:"
but only if there is a "ifeq" right underneath it... how do I do that?
thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 12:51 PM
тАО08-13-2008 12:51 PM
Re: A few sed questions
Regarding your newest question, I think you need to look to Perl or 'awk', so:
# perl -nle 'if (/^ifeq/) {$hold=~s/install/NEWINSTALL/};print $hold if defined $hold;$hold=$_' file
...using this input:
# cat input
install: a b c
ifeq
whatever
endif
install: a b c
ofeq
whatever
endif
install: a b c
ifeq
whatever
endif
...the Perl script yields:
NEWINSTALL: a b c
ifeq
whatever
endif
install: a b c
ofeq
whatever
endif
NEWINSTALL: a b c
ifeq
whatever
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:01 PM
тАО08-13-2008 01:01 PM
Re: A few sed questions
So, it can not be done in sed, but it can be done in perl right?
no problem, I can use perl in the shell script, i just want to be sure!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:02 PM
тАО08-13-2008 01:02 PM
Re: A few sed questions
when you reply, I will submit the right point again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:06 PM
тАО08-13-2008 01:06 PM
Re: A few sed questions
Thanks! Glad to help!!!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:07 PM
тАО08-13-2008 01:07 PM
Re: A few sed questions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:11 PM
тАО08-13-2008 01:11 PM
Re: A few sed questions
> So, it can not be done in sed, but it can be done in perl right?
Well, in fairness, I wouldn't say that it can't be done with 'sed' --- a 'sed' guru probably could, but I'm not that. A good 'sed' guide, including some very advanced handling is here:
http://www.gnu.org/software/sed/manual/sed.html
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 01:32 PM
тАО08-13-2008 01:32 PM
Re: A few sed questions
OK, 'awk' looks like this:
# awk '{if (/^ifeq/) {sub("install","NEWINSTALL",hold)};if (NR>1) {print hold};hold=$0}' file
...with the same example I used for the Perl snippet.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-13-2008 02:02 PM
тАО08-13-2008 02:02 PM
Re: A few sed questions
You are missing the last line:
awk '
{
if (/^ifeq/) {
sub("install","NEWINSTALL",hold)
}
if (NR>1) {print hold}
hold=$0
}
END {print hold}' file