1827304 Members
2820 Online
109961 Solutions
New Discussion

regular expressions

 
SOLVED
Go to solution
M. Rozin
Occasional Advisor

regular expressions

Hi,

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.
13 REPLIES 13
Hakki Aydin Ucar
Honored Contributor

Re: regular expressions

You can use this method:
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

Hakki Aydin Ucar
Honored Contributor

Re: regular expressions

sorry,

sed -e 's/\^kernel/\$notsc/g' temp1 > temp2

if it is OK

cat temp2 > grub.conf
H.Becker
Honored Contributor
Solution

Re: regular expressions

Maybe a this command line

sed -i '/kernel.*notsc/ !{s/^[[:space:]]*kernel.*/& notsc/}' grub.conf

can do the job.
James R. Ferguson
Acclaimed Contributor

Re: regular expressions

Hi Maxim:

# perl -ple 's/(^\s*kernel(?!.*notsc))(.*$)/$1$2\ xxxxx/' grub.conf

Regards!

...JRF...
M. Rozin
Occasional Advisor

Re: regular expressions

First of all, thank you for your reply guys!

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

Re: regular expressions

Hi (again) Maxim:

# 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...



M. Rozin
Occasional Advisor

Re: regular expressions

Thank you James for correcting the mistake. I was wondering about the purpose of xxxx :)

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.
kobylka
Valued Contributor

Re: regular expressions

Hello M.Rozin!

Here a simple sed script to accomplish this task:

sed -n -e '/notsc/{
p
d
}' -e '/^kernel/s/$/notsc/' -e 'p'


Kind regards,

Kobylka
Hakki Aydin Ucar
Honored Contributor

Re: regular expressions

Actually I made a script it was working in shell command by command, but inside sed operator string goes to split ?? it needs to keep strict line to work exactly,
-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

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.