LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed to delete a line for a word and line above (http://www.linuxquestions.org/questions/programming-9/sed-to-delete-a-line-for-a-word-and-line-above-653226/)

cmontr 07-02-2008 10:04 PM

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//

jschiwal 07-02-2008 10:08 PM

I don't think you can use braces in address ranges.

cmontr 07-02-2008 10:09 PM

Quote:

Originally Posted by jschiwal (Post 3202372)
I don't think you can use braces in address ranges.

How would you change it ?

pixellany 07-02-2008 10:14 PM

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

cmontr 07-02-2008 10:15 PM

Quote:

Originally Posted by cmontr (Post 3202373)
How would you change it ?

Tried without them but it wont work...please let me know if you can help..thanks much

cmontr 07-02-2008 10:21 PM

Quote:

Originally Posted by pixellany (Post 3202376)
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

ghostdog74 07-02-2008 11:11 PM

Code:

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

cmontr 07-03-2008 12:17 AM

Quote:

Originally Posted by ghostdog74 (Post 3202427)
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

Mr. C. 07-03-2008 12:41 AM

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.

ghostdog74 07-03-2008 12:44 AM

Quote:

Originally Posted by cmontr (Post 3202499)
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

Mr. C. 07-03-2008 01:04 AM

Fun with perl too:

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

cmontr 07-03-2008 08:33 AM

Quote:

Originally Posted by Mr. C. (Post 3202535)
Fun with perl too:

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

This rules...thanks millions.


All times are GMT -5. The time now is 07:45 PM.