LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Insert a new value (http://www.linuxquestions.org/questions/linux-general-1/insert-a-new-value-931026/)

bnbhp0 02-24-2012 07:07 AM

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

lucmove 02-24-2012 09:29 AM

Try this:

Code:

$ sed 's/SEVERITY/&\nAPPLICATION/' file.txt | sed '/APPLICATION/{n;/APPLICATION/d;}'
The first sed will just add "APPLICATION" to the line after "SEVERITY" no matter what.

The second sed will look for two consecutive "APPLICATION" lines and delete the second one.

firstfire 02-24-2012 09:30 AM

Hi.

Code:

$ cat infile.txt
SEVERITY
APPLICATION
MESSAGE
OBJECT
....
....
SEVERITY
NODENAME
TEXT
MESSAGE
$ sed '/SEVERITY/{N; /\nAPPLICATION/!s/\n/\nAPPLICATION\n/}' infile.txt
SEVERITY
APPLICATION
MESSAGE
OBJECT
....
....
SEVERITY
APPLICATION
NODENAME
TEXT
MESSAGE


bnbhp0 02-24-2012 09:45 AM

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.

firstfire 02-24-2012 09:57 AM

Hi.

It looks like you're using an ancient version of sed which does not support the `\n' escape-sequence.

bnbhp0 02-24-2012 10:32 AM

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.

firstfire 02-24-2012 10:57 AM

Hi.

Code:

$ cat infile.txt
SEVERITY Minor
APPLICATION "Micro"
MESSAGE "OPC
...........
.......
SEVERITY Critical
OBJECT "Micro"
MESSAGE "OPC
......
......
APPLICATION "Micro1"
APPLICATION "Micro2"
......
.......
$ sed '/SEVERITY/{N; /\nAPPLICATION/!s/\n/\nAPPLICATION\n/}' infile.txt | sed '/APPLICATION/{N; s/^APPLICATION.*\n\(APPLICATION\)/\1/}'
SEVERITY Minor
APPLICATION "Micro"
MESSAGE "OPC
...........
.......
SEVERITY Critical
APPLICATION
OBJECT "Micro"
MESSAGE "OPC
......
......
APPLICATION "Micro2"
......
.......

First `sed' command is the same as above. Second `sed' removes first APPLICATION if finds pair of them.

bnbhp0 02-24-2012 11:18 AM

Awesome i got the output what i wanted to.Thanks a lot for your help on this.

jonjgc 02-24-2012 07:39 PM

In case you want an awk version:

awk '
/^SEVERITY/ {
print
getline
if (!/^APPLICATION/)
print "APPLICATION"
}
{print}
' infile.txt

bnbhp0 02-27-2012 09:36 AM

Thanks a lot, both sed and awk worked for me.Thanks again.


All times are GMT -5. The time now is 01:49 AM.