1755645 Members
3353 Online
108837 Solutions
New Discussion юеВ

regular expressions

 
SOLVED
Go to solution
M. Rozin
Occasional Advisor

Re: regular expressions

Thank you everybody for all the suggestions.

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.
James R. Ferguson
Acclaimed Contributor

Re: regular expressions

Hi Maxim:

> 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...
M. Rozin
Occasional Advisor

Re: regular expressions

Thanks for the update James
Dennis Handly
Acclaimed Contributor

Re: regular expressions

>Kobylka - your script doesn't seem to work.

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.