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 |
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.) |
Suppose you have this:
pattern # other stuff pattern junk # stuff pattern stuf stuf #pattern pattern Which lines should be deleted? |
Quote:
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. |
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 |
Quote:
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. |