1835202 Members
2531 Online
110077 Solutions
New Discussion

Sed and awk help needed

 
SOLVED
Go to solution
James Andertom
Esteemed Contributor

Sed and awk help needed

Hi All,

I have been trying to work out a solution using sed and awk on a file but am having no joy. I am hoping someone can help me.

I have a text file which is like the following:

DESCRIPTION "OV_Testing"
CONDITION_ID "nnndjdj"
CONDITION "JDJKDKDK"
$e "jdjdjdj"
$G jsjdj
$S 5606060
SET
SERVERLOGONLY
SEVERITY Normal
NODE IP kkdkdkdk
OBJECT "<$2>"

In the file I need to search for all the occurences of OV_Testing and then change the OBJECT "<$2>" to OBJECT "<$r>".

I can match the OV_Testing without any problems but I do not know how to change the value of OBJECT when I have matched the OV_Testing field in the block of text.

Any help would be greatly appreciated.

Thanks




7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

Re: Sed and awk help needed

# perl -pi.prv -e's/OBJECT "<\$2>"/OBJECT "<\$r>"/g' your_file

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Massimo Bianchi
Honored Contributor

Re: Sed and awk help needed

Or,
let say the file is named goofy


cat goffy | awk '/OV_Testing/, /\$2/' | sed s.\$2.whatdoyouwant. > destination

now destination keeps the modified nodes.

you can merge this with the source goofy, for example using diff .

Just an hint...
HTH,
Massimo


James R. Ferguson
Acclaimed Contributor
Solution

Re: Sed and awk help needed

Hi:

# sed -e '/OV_Testing/,/OBJECT/ s/"<$2>"/"<$r>"/'

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Sed and awk help needed

:) JRF's posting better explained to me what was needed than the quest itself :) [ Or I did bad reading ]

# perl -pi.prv -e'm/^DESCRIPTION "([^"]+)/ and$desc=$1;m/^OBJECT/&&$desc eq"OV_Testing"and s/<\$2>/<\$r>/' your_file

Much better. (sed is shorter, but this was inline editing)

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Sed and awk help needed

Hi Merijn:

Thanks, but of course, I neglected to show that, unlike with perl, no inline editing is possible :-))

Hence, I would write:

# sed -e '/OV_Testing/,/OBJECT/ s/"<$2>"/"<$r>"/' filename > filename.new

# mv filename.new filename

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: Sed and awk help needed

try this

cat file | sed 's/OBJECT \"<\$2>\"/\"<\$r>\"/g' > outfile
All paths lead to destiny
James Andertom
Esteemed Contributor

Re: Sed and awk help needed

Thanks for all the help. Worked great.