LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to search and delete previous line as well (https://www.linuxquestions.org/questions/programming-9/how-to-search-and-delete-previous-line-as-well-773045/)

mallesh1985 12-02-2009 03:06 PM

how to search and delete previous line as well
 
Hi,

I have a question, <<mod edit: no longer relevant>>

Requirements:

I want to search a patterent in a file. If the patteren found in a line - then

case 1) if the previous line (just one line) starts with # then it should be removed along with the current line.

case 2) if the previous line does not start with # then only the current line (where the patteren is found) should be deleted.

case 3) if the pattern is not found - then the line should not be deleted.

I want the above should be done through the awk command. I want to execute the command in the shell script ( not from the command line).

Can you please help for this.

Thanks,
Mallesh

pixellany 12-02-2009 03:34 PM

Moved to a separate thread---the one you posted to is quite old.

This appears to be homework----regardless, please tell us what you have tried. (and why it has to be using AWK.)

pixellany 12-02-2009 03:57 PM

Suppose you have this:

pattern
# other stuff
pattern
junk
# stuff
pattern
stuf
stuf
#pattern
pattern

Which lines should be deleted?

mallesh1985 12-03-2009 10:25 AM

Quote:

Originally Posted by pixellany (Post 3777613)
Suppose you have this:

pattern
# other stuff
pattern
junk
# stuff
pattern
stuf
stuf
#pattern
pattern

Which lines should be deleted?

Hi,

The below all should be getting deleted.

pattern
# other stuff
pattern
# stuff
pattern
#pattern -- This deleted because of next line containing the pattern.
pattern


I was trying to use the below, but couldn't succeeded.

awk '/^#/ {printf line; line=$0"\n"; next} /pattern/ {line=""} ! /pattern/ {printf line; print; line=""}'

Thanks for your help.

pixellany 12-03-2009 10:54 AM

You haven't answered my other questions---including why it has to be Awk.

For the solution you posted, what was the problem?

Here something in SED, but not completely tested:
Code:

sed '/#/{h;n;/pattern/{d;g;d}};/pattern/d' filename > newfilename

mallesh1985 12-03-2009 03:41 PM

Quote:

Originally Posted by pixellany (Post 3778573)
You haven't answered my other questions---including why it has to be Awk.

For the solution you posted, what was the problem?

Here something in SED, but not completely tested:
Code:

sed '/#/{h;n;/pattern/{d;g;d}};/pattern/d' filename > newfilename

I tried using it.. but its not working.

The answer for your question why in awk is I tried with SED but couldnt get the exact command. So I thought it can be done using awk.

Some thing like below I got it now using nwak.

nawk -v pattern=$pattern '/^#/ {printf line; line=$0"\n"; next} $0 ~ pattern {line=""} $0 !~ pattern {printf line; print; lin
e=""}' $filename

it seems it is working now. Doing the complete testing.


All times are GMT -5. The time now is 03:24 PM.