LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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, 11: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, 11:08 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
I don't think you can use braces in address ranges.
 
Old 07-02-2008, 11: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, 11:14 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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, 11: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, 11: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-03-2008, 12:11 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
awk 'BEGIN{RS="flag=1"}RT{$NF=""}1' file
 
Old 07-03-2008, 01: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, 01:41 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
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, 01:44 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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 01:47 AM.
 
Old 07-03-2008, 02:04 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Fun with perl too:

perl -e 'undef $/; $_ = <>; s/.*\nflag=1//g; print "$_"' in
 
Old 07-03-2008, 09: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


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 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-27-2008 12:51 AM
Delete a line range with sed J_Szucs Programming 8 04-30-2008 12:43 AM
Delete a line using sed kushalkoolwal Programming 2 09-29-2007 08:25 PM
using sed to delete line to the right khairilthegreat Linux - Newbie 5 07-28-2007 03:10 PM
trying to delete a line with sed deoren Linux - General 2 01-03-2005 10:26 PM


All times are GMT -5. The time now is 05:28 AM.

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