Sed - Pattern Searching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - Pattern Searching
# 1  
Old 06-15-2009
Sed - Pattern Searching

Hi,

Please take a look at the below eg. I would like to search for abc() pattern first and then search for (xyz) in the next line. If I can find the pattern, then I should delete the 3 lines.

I can only find the pattern and delete but I am unable to find two patterns and delete. Any response is appreciated.

Eg :

Existing file
------------
abc() {
when : (xyz)&a&b&a
}

abc(){
when : a&b&c
}

abc() {
when : a&b&c&(xyz)

Expected Result file
-------------------
abc(){
when : a&b&c
}

Thanks
# 2  
Old 06-15-2009
Code:
use this code where you are searching for the patter without xyz

sed  -n '
/[ ]a&b&c$/ !{
h
}
/[ ]a&b&c$/ {
H
n
H
x
p
} ' input.txt


Last edited by ahmad.diab; 06-15-2009 at 01:32 PM..
# 3  
Old 06-15-2009
if you have Python on your system, an alternative
Code:
#!/usr/bin/env
data=open("file").read().split("\n\n")
for i in data:
    if not "(xyz)" in i:
        print i

output
Code:
# ./test.py
abc(){
when : a&b&c
}

# 4  
Old 06-15-2009
Code:
Upon request from Mr. "rakeshawasthi" and to let the benefit to be spreaded between us I will explain the sed command I had wrote earlier:-

code:
sed -n '
/[ ]a&b&c$/ !{
h      # put the non-matching line in the hold buffer
}
/[ ]a&b&c$/ {
H            # found a line that matches
              # append it to the hold buffer

n             # the hold buffer contains 2 lines
	       # get the next line

H             # and add it to the hold buffer

x              # now print it back to the pattern space

p              # and print it.

} ' input.txt


Last edited by ahmad.diab; 06-15-2009 at 01:32 PM..
# 5  
Old 06-16-2009
Code:
$/="\n\n";
while(<DATA>){
	next if /abc.*\(xyz\).*/s;
	print;
}
__DATA__
abc() {
when : (xyz)&a&b&a
}

abc(){
when : a&b&c
}

abc() {
when : a&b&c&(xyz)
}

# 6  
Old 06-16-2009
Thanks ahmad.diab!!! Thanks a lot.
# 7  
Old 06-16-2009
The following sed command should do the job:

Code:
 sed '/abc()/{$!N;/\n.*(xyz)/{$!N;d;}}' filename

When the pattern abc() is not found in the line, it is printed (standard sed behaviour) Otherwise, $!N;/\n.*(xyz)/{$!N;d;} command is executed.
$!N appends next line of input file to the current pattern buffer if it is not the end of file already.
So after execution, we have something like previous_line/ncurrent_line in it. Then we search pattern '\n.*(xyz)' in the buffer, to emulate searching the line following the one with abc(). If the (xyz) pattern is found, $!N;d; command is executed, which appends the third line of input to the pattern buffer and then discards it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Searching and printing the only pattern using awk,sed or perl

Hi All, i have an output of command vmstat as below : $ vmstat System configuration: lcpu=4 mem=5376MB ent=1.00 kthr memory page faults cpu ----- ----------- ------------------------ ------------ ----------------------- r b avm fre re pi... (10 Replies)
Discussion started by: omkar.jadhav
10 Replies

3. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

4. Shell Programming and Scripting

pattern searching

Hi, Can you please help me out here? I am trying develop a search pattern to extract certain words from the two strings below. I want to extract ericsson_msc_live from the 2 strings and then the date, which is a part of the filename just before the .jar extension. ... (19 Replies)
Discussion started by: danish0909
19 Replies

5. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

6. Shell Programming and Scripting

Searching for a pattern

How do I search for a pattern - N/A in a particular column using awk? (11 Replies)
Discussion started by: rabiu
11 Replies

7. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

8. Shell Programming and Scripting

Pattern searching pattern in c files

I have a problem in searching a specific pattern in c files. My requirement: I have to find all the division operator in all cfiles. The problem is, the multi line comments and single line comments will also have forward slash in it. Even after avoiding these comments also, if both... (6 Replies)
Discussion started by: murthybptl
6 Replies

9. Shell Programming and Scripting

Regarding Searching Pattern

Hi Guys, Can you help with the shell script: I would like to search a fixed width pattern from a file say for each line from a fixed position and lenght it has to return all rows from the file. Example: To search the third column for "def" it has to return 1 and 4th rows only ... (2 Replies)
Discussion started by: sbasetty
2 Replies

10. UNIX for Dummies Questions & Answers

How to use cat command in sed pattern searching

Hi All, I have a file named pattern.dat which contains pattern like A1000090 250.00 250.00 i have one more file named test.dat in which this pattern is present. What i should do is, in test.dat after this pattern i should append comments. i used... (4 Replies)
Discussion started by: Sona
4 Replies
Login or Register to Ask a Question