- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- regular expressions
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
Forums
Discussions
Discussions
Discussions
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
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
09-26-2009 03:14 AM
09-26-2009 03:14 AM
Can somebody help me with a sed and regular expressions?
I need a script that will append 'notsc' to every line in a file that starts with 'kernel' but does not contain 'notsc'.
I tried the following but it doesn't work:
sed -i '/^[[:space:]]*kernel(?!notsc)/s|$| notsc|' grub.conf
Thanks,
Maxim.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2009 12:41 PM
09-26-2009 12:41 PM
Re: regular expressions
First use grep to find out 'kernel' without 'notsc' ;
cat grub.conf | grep -v notsc | grep ^kernel > temp1
sed -e 's/\^kernel/\$notsc/' grub.conf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2009 12:43 PM
09-26-2009 12:43 PM
Re: regular expressions
sed -e 's/\^kernel/\$notsc/g' temp1 > temp2
if it is OK
cat temp2 > grub.conf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2009 01:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2009 04:10 PM
09-26-2009 04:10 PM
Re: regular expressions
# perl -ple 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ xxxxx/' grub.conf
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2009 05:22 PM
09-26-2009 05:22 PM
Re: regular expressions
Hakki - I think that your suggestion will leave only the kernel lines in grub.conf, while I want to leave the file as is, and just to append 'notsc' to some of the lines.
H - I will try you command at work and will let you know whether it works.
James - I asked for a sed command because this script should run on newly installed systems and I am not 100% sure that perl is part of the default installation. But anyway, I will try it at work and will let you know.
Thanks,
Maxim.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2009 06:49 AM
09-27-2009 06:49 AM
Re: regular expressions
# perl -ple 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ xxxxx/' grub.conf
...should have been:
# perl -ple 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ notsc/' grub.conf
The 'xxxxx' was what I used for testing as it simplified spotting good and bad cases.
> "I am not 100% sure that perl is part of the default installation."
Interesting point. If I recall correctly, Perl is part of Debian, Ubuntu, OpenSUSE and definitely Fedora. I'd be very interested to know your findings.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2009 07:17 AM
09-27-2009 07:17 AM
Re: regular expressions
In our case it's RHEL, but we have a customized installation, so I have to check whether perl is part of this installation.
I will check it on Tuesday, when I'll be in the office and will let you know.
Maxim.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2009 11:49 PM
09-27-2009 11:49 PM
Re: regular expressions
Here a simple sed script to accomplish this task:
sed -n -e '/notsc/{
p
d
}' -e '/^kernel/s/$/notsc/' -e 'p'
Kind regards,
Kobylka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2009 04:35 AM
09-28-2009 04:35 AM
Re: regular expressions
-May be somebody will come up a solution to make working a string inside a sed ??
test it you will see the problem :
#!/bin/sh
cat testfile > test2file
line2=" notsc"
cat test2file |grep -i kernel |grep -v notsc | while read line
do
sed "s/$line/$line $line2/" test2file > test3file
cp test3file test2file
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2009 10:06 PM
09-29-2009 10:06 PM
Re: regular expressions
Hakki - you script might do the job, but I am looking for a more "elegant" solution. Thank you for the effort anyway.
Kobylka - your script doesn't seem to work.
James - your perl suggestion does the job, but it prints the contents to standard output rather than replacing the actual file. It's probably easy to change it to replace the file, but I am not a big perl expert. BTW, our default RHEL installation does include perl.
And the winner is H.Becker. This is exactly what I need. Here is the exact command that I am going to use:
sed -i '/^[[:space:]]*kernel.*notsc.*/ !{s/^[[:space:]]*kernel.*/& notsc/}' grub.conf
Have a great day,
Maxim.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2009 03:49 AM
09-30-2009 03:49 AM
Re: regular expressions
> James - your perl suggestion does the job, but it prints the contents to standard output rather than replacing the actual file
That's easy to make happen. We simply add the '-i' switch to enable in-place editing and optionally an argument with it to use as a suffix for a backup copy of the unmodified file. In all:
# perl -ple 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ notsc/' grub.conf
...becomes:
# perl -pi.old -e 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ notsc/' grub.conf
The 'grub.conf' will be modified in situ while the unmodified file will be present as 'grub.conf.old'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2009 05:27 AM
09-30-2009 05:27 AM
Re: regular expressions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2009 10:20 PM
11-16-2009 10:20 PM
Re: regular expressions
It seems to work fine for me. If you provide a file, it writes the output to stdout. Though -i option doesn't work with -n.