Visit Our UNIX and Linux User Community


sed REGEX to print multiple occurrences of a pattern from a line


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed REGEX to print multiple occurrences of a pattern from a line
# 1  
Old 08-20-2015
sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it.

Example line:

Code:
getInfoCall: [ call=1 origin=AAAA ] info received please proceed, getInfoCall: [ call=3 origin=BBBB ] info received please proceed, getInfoCall: [ call=5 origin=CCCC ] info received please proceed, getInfoCall: [ call=2 origin=DDDD ] info received please proceed, getInfoCall: [ call=4 origin=EEEE ] info received please proceed

And I need to pull only the "call=1 origin=EEEE" patterns from this line. Like

Code:
call=1 origin=AAAA
call=2 origin=DDDD
call=3 origin=BBBB
call=4 origin=EEEE
call=5 origin=CCCC

I know we can do it with awk easily. But trying to know if there is a way we can try with SED regex.

Tried below and got only the last part of it.

Code:
echo "above string" | sed -e 's/.*\(call=[0-9]\) \(origin=[A-Z]\{4\}\).*/\1 \2/g'

call=4 origin=EEEE

Please help. Smilie
# 2  
Old 08-20-2015
Hello Vidyaprakash,

Following awk solution, may help you in same.
Code:
 awk '($0 == "call=4 origin=EEEE")'  Input_file


Thanks,
R. Singh
# 3  
Old 08-20-2015
Try
Code:
sed 's/^[^c]*\(call\)/\1/;s/ \(call\)/\n\1/g;s/\(origin=[^ ]*\)[^\n]*/\1/g' file
call=1 origin=AAAA
call=3 origin=BBBB
call=5 origin=CCCC
call=2 origin=DDDD
call=4 origin=EEEE

# 4  
Old 08-20-2015
Code:
sed 's#^[^[][^[]*\[##g;s#][^[]*##g' myFile | tr '[' '\n'


Last edited by vgersh99; 08-20-2015 at 02:36 PM..
# 5  
Old 08-20-2015
The following needs two lines
Code:
sed 's/[^[]*\[ *\([^]]*\) *\][^[]*/\1\
/g' file

GNU sed can take \n here
Code:
sed 's/[^[]*\[ *\([^]]*\) *\][^[]*/\1\n/g' file

Just realized, the [^]] include the trailing spaces; the following * does not delete them.
And how to get rid of the blank line?
Well, either remove the embedded \n at the end of the line buffer
Code:
sed 's/[^[]*\[ *\([^]]*\)\][^[]*/\1\
/g; s/\n$//' file

Or, move the newline before the match, and remove the first embedded \n
Code:
sed 's/[^[]*\[ *\([^]]*\)\][^[]*/\
\1/g; s/\n//' file


Last edited by MadeInGermany; 08-20-2015 at 03:39 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #391
Difficulty: Medium
Darwin, the open source subset of macOS, has behavior that can be set to comply with UNIX 03.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like Device ID: j01-01, IP address: 10.10.10.36, IP address: 10.10.10.35, IP address: 10.10.102.201, Platform: 8040, Capabilities: Host , Interface: GigabitEthernet9/45, Port ID (outgoing port): e0k,Here is what I have so far but it... (4 Replies)
Discussion started by: dis0wned
4 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Shell Programming and Scripting

Print occurrences for pattern match

Hi All, I want to print all the occurrences for a particular pattern from a file. The catch is that the pattern search is partial and if any word in the file contains the pattern, that complete word has to be printed. If there are multiple words matching the pattern on a specific line, then all... (2 Replies)
Discussion started by: decci_7
2 Replies

6. Shell Programming and Scripting

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

7. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

8. Shell Programming and Scripting

Sed print range of lines between line number and pattern

Hi, I have a file as below This is the line one This is the line two <\XMLTAG> This is the line three This is the line four <\XMLTAG> Output of the SED command need to be as below. This is the line one This is the line two <\XMLTAG> Please do the need to needful to... (4 Replies)
Discussion started by: RMN
4 Replies

9. Shell Programming and Scripting

CSV: Replacing multiple occurrences inside a pattern

Greatings all, I am coming to seek your knowledge and some help on an issue I can not currently get over. I have been searching the boards but did not find anything close to this matter I am struggling with. I am trying to clean a CSV file and make it loadable for my SQL*Loader. My problem... (1 Reply)
Discussion started by: OCanada
1 Replies

10. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

Featured Tech Videos