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 12-02-2009, 03:06 PM   #1
mallesh1985
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
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

Last edited by pixellany; 12-02-2009 at 03:35 PM.
 
Old 12-02-2009, 03:34 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.)
 
Old 12-02-2009, 03:57 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Suppose you have this:

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

Which lines should be deleted?
 
Old 12-03-2009, 10:25 AM   #4
mallesh1985
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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.
 
Old 12-03-2009, 10:54 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 12-03-2009, 03:41 PM   #6
mallesh1985
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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.
 
  


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
How to delete previous installation? alikhan Linux - Newbie 1 03-29-2009 12:35 AM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
Unix command to delete the previous and next line of the searched pattern navin29 Linux - Newbie 7 03-14-2008 01:23 PM
How do you delete previous kernels? r3dhatter Debian 5 06-16-2004 01:53 AM

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

All times are GMT -5. The time now is 02:52 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