Insert a new value
Hi All,
MY requirement is, whenever i find key word as "SEVERITY" it has to check the next line and if next line is not "APPLICATION" then it has to insert the value as "APPLICATION". can someone help me how we can do this using sed/awk/perl. My text file looks like this: SEVERITY APPLICATION MESSAGE OBJECT .... .... SEVERITY NODENAME TEXT MESSAGE |
Try this:
Code:
$ sed 's/SEVERITY/&\nAPPLICATION/' file.txt | sed '/APPLICATION/{n;/APPLICATION/d;}' The second sed will look for two consecutive "APPLICATION" lines and delete the second one. |
Hi.
Code:
$ cat infile.txt |
When i try this command:
sed 's/SEVERITY/&\nAPPLICATION/' file.txt | sed '/APPLICATION/{n;/APPLICATION/d;}' i go the output somthinglike below: SEVERITYnAPPLICATION I want it to be printed on next and line something like below SEVERITY APPLICATION and also i dont want "Application" to be deleted if it is already there. When i executed this command: sed '/SEVERITY/{N; /\nAPPLICATION/!s/\n/\nAPPLICATION\n/}' infile.txt it throws me an error: sed: Function /SEVERITY/{N; /\nAPPLICATION/!s/\n/\nAPPLICATION\n/} cannot be parsed. |
Hi.
It looks like you're using an ancient version of sed which does not support the `\n' escape-sequence. |
Thanks you are right i ran below command in different box and it looks good.
sed 's/SEVERITY*/&\nAPPLICATION/' logfile_micro.txt | sed '/APPLICATION/{n;/APPLICATION/d;}' I have one more issue in my text files (sorry i didnt notice this at first instance) I have few other entries which has something like below: SEVERITY Minor APPLICATION "Micro" MESSAGE "OPC ........... ....... SEVERITY Critical OBJECT "Micro" MESSAGE "OPC ...... ...... ...... ....... When it execute above command it gives an out put: SEVERITY APPLICATION Critical I want Critical to be with SEVERITY and in next line is should have APPLICATION also if i find two consecutive "APPLICATION" then it has to delete the one which is at first. |
Hi.
Code:
$ cat infile.txt |
Awesome i got the output what i wanted to.Thanks a lot for your help on this.
|
In case you want an awk version:
awk ' /^SEVERITY/ { getline if (!/^APPLICATION/) print "APPLICATION" } {print} ' infile.txt |
Thanks a lot, both sed and awk worked for me.Thanks again.
|
All times are GMT -5. The time now is 02:01 AM. |