1752301 Members
5320 Online
108786 Solutions
New Discussion юеВ

Re: 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