LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2008, 10:04 PM   #1
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Rep: Reputation: 15
sed to delete a line for a word and line above


Hello guys,

Lets say I have a big file that has text like


o=abc.com
flag=1
o=abcd.com
flag=1
o=abcde.com
flag=1
o=abcdefgh.com
o=abcdef.com
.
.
.

I want to remove every line contains 'flag=1' and a line above it...I used this but got an error machine on solaris machine. ANy ideas please? Thanks again...


sed '/^.* {/ {:a /}/ !{N;ba} }; s/.*flag=1*//' ifile > ofile.txt

Error:
Label too long: /^{/ {:a /}/ !{N;ba} }; s/flag=1//
 
Old 07-02-2008, 10:08 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I don't think you can use braces in address ranges.
 
Old 07-02-2008, 10:09 PM   #3
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jschiwal View Post
I don't think you can use braces in address ranges.
How would you change it ?
 
Old 07-02-2008, 10:14 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
How about something like this:
read a line
send to the hold buffer
read the next line
If it has the desired pattern, then delete it, pull the line from the hold buffer and delete it
then continue

Really good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 07-02-2008, 10:15 PM   #5
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cmontr View Post
How would you change it ?
Tried without them but it wont work...please let me know if you can help..thanks much
 
Old 07-02-2008, 10:21 PM   #6
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by pixellany View Post
How about something like this:
read a line
send to the hold buffer
read the next line
If it has the desired pattern, then delete it, pull the line from the hold buffer and delete it
then continue

Really good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
thanks for the nice link but I couldnt get what I wanted to do yet....please let me know if you can help me with it
 
Old 07-02-2008, 11:11 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{RS="flag=1"}RT{$NF=""}1' file
 
Old 07-03-2008, 12:17 AM   #8
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk 'BEGIN{RS="flag=1"}RT{$NF=""}1' file
H1 again, ..I tried to fix the syntax but still getting the same error...

awk: syntax error near line 1
 
Old 07-03-2008, 12:41 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
It works just fine:

Code:
$ cat in
1
2
flag=1
3
4

$ awk 'BEGIN{RS="flag=1"}RT{$NF=""}1' in
1

3
4
Awk has terrible error diagnostics.
 
Old 07-03-2008, 12:44 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by cmontr View Post
H1 again, ..I tried to fix the syntax but still getting the same error...

awk: syntax error near line 1
if you are on Solaris, use nawk however RT is not supported ( i believe ). RT is supported by GNU awk. another way is to save each line in array, then go through the array, detecting flag=1, and then delete the one item before. Finally print them out. Have a go at it. good luck

Last edited by ghostdog74; 07-03-2008 at 12:47 AM.
 
Old 07-03-2008, 01:04 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Fun with perl too:

perl -e 'undef $/; $_ = <>; s/.*\nflag=1//g; print "$_"' in
 
Old 07-03-2008, 08:33 AM   #12
cmontr
Member
 
Registered: Sep 2007
Posts: 175

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Mr. C. View Post
Fun with perl too:

perl -e 'undef $/; $_ = <>; s/.*\nflag=1//g; print "$_"' in
This rules...thanks millions.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-26-2008 11:51 PM
Delete a line range with sed J_Szucs Programming 8 04-29-2008 11:43 PM
Delete a line using sed kushalkoolwal Programming 2 09-29-2007 07:25 PM
using sed to delete line to the right khairilthegreat Linux - Newbie 5 07-28-2007 02:10 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 09:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 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
Open Source Consulting | Domain Registration