LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 02-24-2012, 07:07 AM   #1
bnbhp0
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Rep: Reputation: Disabled
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
 
Old 02-24-2012, 09:29 AM   #2
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian Wheezy, Slackware
Posts: 604

Rep: Reputation: 70
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.
 
Old 02-24-2012, 09:30 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
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
 
Old 02-24-2012, 09:45 AM   #4
bnbhp0
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-24-2012, 09:57 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hi.

It looks like you're using an ancient version of sed which does not support the `\n' escape-sequence.
 
Old 02-24-2012, 10:32 AM   #6
bnbhp0
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
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.

Last edited by bnbhp0; 02-24-2012 at 10:34 AM.
 
Old 02-24-2012, 10:57 AM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
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.

Last edited by firstfire; 02-24-2012 at 11:01 AM. Reason: Fix a bug.
 
Old 02-24-2012, 11:18 AM   #8
bnbhp0
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Awesome i got the output what i wanted to.Thanks a lot for your help on this.
 
Old 02-24-2012, 07:39 PM   #9
jonjgc
LQ Newbie
 
Registered: Nov 2009
Posts: 17

Rep: Reputation: 6
In case you want an awk version:

awk '
/^SEVERITY/ {
print
getline
if (!/^APPLICATION/)
print "APPLICATION"
}
{print}
' infile.txt
 
Old 02-27-2012, 09:36 AM   #10
bnbhp0
LQ Newbie
 
Registered: Feb 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Thanks a lot, both sed and awk worked for me.Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Ctrl-Insert and Shift-Insert don't work val3xiv Linux - General 2 06-30-2011 06:17 PM
insert lines into a file after calculating where to insert xonar Programming 13 12-24-2009 05:37 AM
How to insert at startup in FC 9? asahlot Linux - Software 9 01-01-2009 07:36 AM
Please insert CD4. . . armasnavi Mandriva 3 01-30-2005 03:59 PM
Please insert Disk 4? kaega2 Mandriva 6 06-19-2004 02:13 AM


All times are GMT -5. The time now is 05:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration