Print mutliple patterns in a line using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print mutliple patterns in a line using sed
# 1  
Old 05-01-2013
Print mutliple patterns in a line using sed

Hi,

I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern.

If the line is
Quote:
<TRANSFORMATION TYPE ="Lookup Procedure" TYPE ="Stored proc">
the the output should be
Lookup Procedure|Stored proc

But the output I am getting is

Stored proc

The code I am using is

Code:
echo '<TRANSFORMATION TYPE ="Lookup Procedure" TYPE ="Stored proc">' | sed 's/.*TYPE ="\([^"]*\)"/\1/'

Plz help.

Thanks

Last edited by kedar_laveti; 05-01-2013 at 09:29 AM..
# 2  
Old 05-01-2013
If perl is an option, try this:
Code:
echo  '<TRANSFORMATION TYPE ="Lookup Procedure" TYPE ="Stored proc">' | perl -ne '@s = $_ =~ /TYPE ="([^"]*)/g; print join ("|", @s)'

# 3  
Old 05-01-2013
Another pearl:
Code:
perl -ne 'while (/(TYPE =".*?")/g){print "$1|"}'

A grep:
Code:
grep -o "TYPE =\"[^\"]*\"" | paste -d'|' -s


Last edited by balajesuri; 05-01-2013 at 10:59 AM..
# 4  
Old 05-01-2013
Code:
echo '<TRANSFORMATION TYPE ="Lookup Procedure" TYPE ="Stored proc">' | sed 's/.*TYPE ="\([^"]*\)"/\1/'

You need to have a regular expression that saves the value after both "TYPE=" in your line. You only save one.


Code:
echo '<TRANSFORMATION TYPE ="Lookup Procedure" TYPE ="Stored proc">' |sed 's/^.* TYPE ="\([^"]*\)" TYPE ="\([^"]*\)">$/\1\|\2/'


Last edited by rwuerth; 05-01-2013 at 11:43 AM.. Reason: grammar
# 5  
Old 05-01-2013
Thanks for the help, but I dont have much idea about perl.
I am finding it difficult when the line is as shown below:

Code:
<TRANSFORMFIELD LKP.LKP_GID_ERROR_LOOKUPS(&apos;P3_TRN_AMT_ZERO&apos;),2, &apos;| &apos;||:LKP.LKP_GID_ERROR_LOOKUPS(&apos;P3_INVALID_TRAN_TYP&apos;),&#xD;&#xA;NULL)"/>

I get the output as

Code:
P3_TRN_AMT_ZERO&apos;),2, &apos;| &apos;||:LKP.LKP_GID_ERROR_LOOKUPS(&apos;P3_INVALID_TRAN_TYP&apos;)

The command I used is :

Code:
perl -ne '@s = $_ =~ /[(]&apos;([^"]*&apos;[)])/g; print join ("|", @s)'

The idea is to print the word between a search string

Thanks
# 6  
Old 05-01-2013
Can't we do it using sed or awk?
# 7  
Old 05-01-2013
try this:
Code:
perl -ne '@s = $_ =~ /\(&apos;(.*?)&apos;\)/g; print join ("|", @s)'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print two matched patterns only from each line?

My input looks like this. # Lot Of CODE Before AppType_somethinglese=$(cat << EOF AppType_test1='test-tool/blatest-tool-ear' AppType_test2='test/blabla-ear' # Lot Of CODE After I want to print text betwen 1) _ and = and 2)/ and ' from each line and exclude lines with "EOF". Output... (2 Replies)
Discussion started by: kchinnam
2 Replies

2. Shell Programming and Scripting

How to print line if two lines above it matches patterns.?

Hi, I could only find examples to print line before/after a match, but I'd need to print line after two separate lines matching. E.g.: From the below log entry, I would need to print out the 1234. This is from a huge log file, that has a lot of entries with "CLIENT" and "No" entries (+ other... (3 Replies)
Discussion started by: Juha
3 Replies

3. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Shell Programming and Scripting

sed print between 2 patterns only last occurence

Hi, I have a file, which contains the following log data. I am trying to print fromt he file the following data: I have tried using sed, but I am getting from the first pattern Thanks for your help. (5 Replies)
Discussion started by: sol_nov
5 Replies

6. Shell Programming and Scripting

How to print the next line by searching with different patterns in AIX server?

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which are... (4 Replies)
Discussion started by: tejastrikez
4 Replies

7. Shell Programming and Scripting

print the next line by searching with different patterns

Hi, I am having an '.xml' file with 'n' number of lines and also having another file with '.txt' format contains values which i want to search. Now I want to print the next line with the pattern which i am searching in '.xml' file. And the loop has to repeat for different patterns which... (5 Replies)
Discussion started by: tejastrikez
5 Replies

8. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

9. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

10. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies
Login or Register to Ask a Question