SED multiple pattern matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED multiple pattern matching
# 1  
Old 04-07-2012
Question SED multiple pattern matching

Hello sorry for the probably simple question - searching about the forums and Internet, I have not found the answer.

Could you tell me please how to do a multiple pattern match with SED

So it would be SED searching for "PATTERN1" 'or' "PATTERN2" not 'and' if they happen to fall on the same line well that is ok to.

so far I have this from the Internet
Code:
sed -e '/PATTERN1/b' -e '/PATTERN2/b' -e d

Is there a better way of writing this? I would like to search on paragraphs containing PATTERN1 or PATTERN2 and delete a blank line. The below SED works for one PATTERN but I would like to search for a possible occurrence of PATTERN2 aswell incase it is there.

Code:
sed '/PATTERN/{N;s/\n//;}'

would something like this work for searching PATTERN1 or PATTERN2
Code:
sed -e '/PATTERN1/{N;s/\n//;}d; -e /PATTERN2/{N;s/\n//;}d' filename

Thanks in advance

Last edited by Scrutinizer; 04-08-2012 at 01:41 AM.. Reason: Some grammer problems; removed font formatting
# 2  
Old 04-07-2012
I think you are close, if I understand what you want to do.

What I think your goal is: when there is a record of text which contains either pattern1 or pattern2, you want to delete the blank line that follows. This is a slight change to your sed script which should do that:

Code:
sed -e '/pattern1/{N;s/\n\n/\n/;}'  -e '/pattern2/{N;s/\n\n/\n/;}' file

There is no need for the delete, and it handles the case where the matching record is NOT followed by a blank line. It does this by replacing two consecutive newlines (after the read of the next line by the N command) with a single newline. If the next line isn't "blank," things are unchanged.

Last edited by agama; 04-07-2012 at 11:23 PM.. Reason: left my testing patterns in -- replaced with generic
# 3  
Old 04-08-2012
Thankyou very much
Will that change do the same for all instances of those patterns searched?
In the files I search, there are many instances of one or the other of the patterns, I assume this will progress through the entire file?

Thankyou for taking the time to reply, this helped greatly. I will try this on the server script I am running.
Regards
# 4  
Old 04-08-2012
Quote:
Originally Posted by lostincashe
Will that change do the same for all instances of those patterns searched?
In the files I search, there are many instances of one or the other of the patterns, I assume this will progress through the entire file?
Regards
You are correct, it will apply the change to each record in the file with either of the patterns.
# 5  
Old 04-08-2012
Bug

Many thanks again Agama
Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use sed to get first matching pattern

HI, I have a file: listenerport: - 1521 - 10520 - 10521 - 10522 - 10523 instances: listenerport: listenerport: - 1521 - 10540 - 10541 - 10542 - 10543 instances: ... (5 Replies)
Discussion started by: netbanker
5 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Help with sed pattern matching

Hi My log file is Testtmp2 cat Testtmp2 12:12:38 12:14:29 12:17:34 12:19:08 12:20:10 12:21:35 12:22:20 12:22:26 12:22:34 12:22:38 12:28:14 12:31:35 12:32:50 12:33:04 (3 Replies)
Discussion started by: rahkumar
3 Replies

4. Shell Programming and Scripting

help! Pattern Matching in vi or sed

I have a bunch of conf files, that contain the fully qualified names of servers. I would like to be able to use some sort of pattern matching with sed or vi, or whatever, to pull out the fully qualified server names, and dump them in a file. It just needs to work across several unix os. So, I... (4 Replies)
Discussion started by: tabini
4 Replies

5. Shell Programming and Scripting

SED pattern matching help

Hello All, I have the following lines in a file <address location="test" ConnectionName="test" /> I want to replace the above lines by <address location="test123" /> I am usind SED and not able to remove the new line characters between the two lines. Can anyone please help... (4 Replies)
Discussion started by: ramk
4 Replies

6. Shell Programming and Scripting

sed pattern matching

Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: - https://www.unix.com/shell-programming-scripting/123118-append-position-28-33-a.html He was asking if you can use sed to match a pattern you want to replace within a... (6 Replies)
Discussion started by: steadyonabix
6 Replies

7. Shell Programming and Scripting

sed pattern matching

Hi all, if I have the following piece (repeating) of text within a file and I wish to delete it via sed a b c d e <tr> <td><img alt="" height="1" width="3" src="/testweb/view/browser/images/shim.gif"></td><td><img alt="" height="1" ... (4 Replies)
Discussion started by: srage
4 Replies

8. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

9. Shell Programming and Scripting

sed pattern matching

Hi, I have a file which contains a word like ravi and ravi30. i want to replace only the word ravi with xxx for that i am using the below sed command sed -e 's/ravi/xxx/g' . but the above command out put is xxx and xxx30 but i dont need to change ravi30 please guide me how to proceed.... (4 Replies)
Discussion started by: ravi_rn
4 Replies

10. Shell Programming and Scripting

Pattern matching sed

MSG="THERE WERE XX RECORDS IN ERROR TABLE,AAAA, WHEN LOADING THE BBBB TABLE WITH EXTRACT FROM CCCC INTO TABLES FOR , DATABASE DDDD." echo "$MSG" > /tmp/mplanmsg.$$.out I wan to replace XX with the content in $recordXX cat /tmp/mplanmsg.$$.out|sed 's/XX/\$recordXX/g'| sed... (3 Replies)
Discussion started by: leemjesse
3 Replies
Login or Register to Ask a Question