Multiple pattern search in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple pattern search in perl
# 1  
Old 08-03-2011
Multiple pattern search in perl

Code:
user 10
values
content is:
musage.py
yes
value

user 11
values
content is:
gusage.py
yes
value

how to print "user" string line by searching "content is:" string and "usage.py" string in perl

Last edited by pludi; 08-03-2011 at 11:05 AM..
# 2  
Old 08-03-2011
what is the output you are expecting in the sample you provided?

Y only "perl" and not others? sed,awk , normal bash etc?
# 3  
Old 08-03-2011
Are you expecting a script that you can supply 2 arguments to:
Code:
my_script.pl user_data.dat  nusage.py

which returns all users with nusage.py in their values list?
Code:
~/ $ perl -e 'open DATA , "<", "tmp.dat";while(<DATA>){ if (/^user\s+(\d+)$/){ $uid=$1;}if(/$ARGV[0]/){print "user $uid matches\n"}}' gusage.py
user 11 matches
~/$

# 4  
Old 08-03-2011
I think the TS meant "or" instead of "and":
Code:
perl -lne '/content is:|usage\.py/ && print' INPUTFILE
content is:
musage.py
content is:
gusage.py

# 5  
Old 08-03-2011
I know how to do in sed by using hold buffer, but i want it in perl. I dont want to supply as command line arguments.

Output should be: user 10 or user 11.

I should search for "content is:" and musage.py it should print user 10.
similarly if I search for "content is:" and gusage.py it should print user 11.
Yes I know for this I may need two if loops.

Please let me know
# 6  
Old 08-03-2011
Try:
Code:
perl -00 -ne '/content is:/ && /usage.py/ && /.*/ && print "$&\n"' INPUTFILE

# 7  
Old 08-03-2011
actually file is

Quote:
intro
global: user 10
values
content is:
musage.py
yes
value

intro
global: user 11
values
content is:
gusage.py
yes
value
I want "content is:", "usage.py" and "global" to be searched but "global: user 10" and "global: user 11" should be printed.

Above one liner is not working if the file is like this.
Please let me know..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern search multiple files

#!/usr/bin/ksh a="Run successfully" cd $APPS ls -l *.txt | while read $txt do if then cp $APPS/$txt cp $hist/$txt else rm $APPS/$txt echo "Files has been removed" fi done New in shell script please help me out Around 100 txt files in $APPS dir i want to search pattern from... (8 Replies)
Discussion started by: Kalia
8 Replies

2. Shell Programming and Scripting

Search multiple pattern in a file

I have a sample file with following output: HTTP/1.1 200 OK User: admin Set-Cookie: AMBARISESSIONID=y3v3648yqcno32nq478kw7ar;Path=/;HttpOnly Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/plain Vary: Accept-Encoding, User-Agent Content-Length: 6057 Server:... (4 Replies)
Discussion started by: saurau
4 Replies

3. Linux

Search multiple pattern from list

I am working on AIX operating system. I want to search list of Article Id for given Set Date (which are present in a seperate file input.txt) art_list.csv ------------ "Article ID" |"Ad Description" |"Pyramid"|"Pyramid Desc "|"ProductTypeId"|"Set Date "|... (3 Replies)
Discussion started by: rajivrsk
3 Replies

4. Shell Programming and Scripting

awk with multiple pattern search

Want to fetch a column with multiple pattern using awk. How to achieve the same. Tried cat test address : 10.63.20.92/24 address : 10.64.22.93/24 address : 10.53.40.91/24 cat test | awk '{print $3}' |awk -F "/" '{print $1}' 10.63.20.92 10.64.22.93 10.53.40.91 Is there any... (2 Replies)
Discussion started by: Manasa Pradeep
2 Replies

5. Shell Programming and Scripting

Multiple search pattern

Hello :) I have this file cat employee_list Name : jack Gender: m ID : 4512 DOB : 03/27/1980 hire date : 04/23/2012 Nationality: US marital status : single ===================== Name : mick Gender: m ID : 1256 DOB : 03/27/1970 Hire date : 012/10/2011 Nationality: US Marital... (4 Replies)
Discussion started by: Sara_84
4 Replies

6. Shell Programming and Scripting

Help need with PERL multiple search pattern matching!

My example file is as given below: conn=1 uid=oracle conn=2 uid=db2 conn=3 uid=oracle conn=4 uid=hash conn=5 uid=skher conn=6 uid=oracle conn=7 uid=mpalkar conn=8 uid=anarke conn=9 uid=oracle conn=1 op=-1 msgId=-1 - fd=104 slot=104 LDAPS connection from 10.10.5.6 to 10.18.6.5 conn=2... (3 Replies)
Discussion started by: sags007_99
3 Replies

7. Shell Programming and Scripting

search more than one pattern with perl on same line

Hi friends, I want to search for some hex error codes in some files. After the hex error code is found, the occurences would be counted. Afterwards the found hex errorcode would be cat into a separate file. Here is my code: #!/usr/bin/perl use File::Basename; my $find = $ARGV; my... (2 Replies)
Discussion started by: sdohn
2 Replies

8. Shell Programming and Scripting

perl pattern search

can someone help me out with the bolded? else if (regmatch($Subject, "^Application") && (regmatch($From, "^etgh") && (regmatch($Body, ".*not authorized to use this server.*")))) what this section of the code is suppose to do is to scan through the contents of $Body, if do a set of... (1 Reply)
Discussion started by: SkySmart
1 Replies

9. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

10. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies
Login or Register to Ask a Question