Operating System - HP-UX
1753870 Members
7402 Online
108809 Solutions
New Discussion юеВ

Re: Search pattern from a file

 
SOLVED
Go to solution
Rahul_13
Advisor

Search pattern from a file

Hi,

I have to search a particular pattern in a file and extract only the pattern and the word that follows the pattern separated with colon. For example if the file contains a line

....................abc : def...............

I want to pick only "abc : def" from the line.

Can anyone please help me out in this issue.

Thanks,
Rahul

10 REPLIES 10
Sundar_7
Honored Contributor

Re: Search pattern from a file

Try this

grep " abc :" inputfile | sed 's/.*\( abc :\)\( .* \).*/\1\2/'

Learn What to do ,How to do and more importantly When to do ?
Hein van den Heuvel
Honored Contributor

Re: Search pattern from a file

Or with perl:

perl -ne 'print "$1\n" if (/(abc :\s+\w+)/)' inputfile

-ne = loop over input file and program to follow'
$1 = matching text remembered between parentheses.
abc : = primary mathc string
\s+\w+ = secondary match = some white space followed by one or more 'word' characters (a-z,A-Z,0-9,_)

You may need to define more clearly what a word means to you if this does not work.

Hein.

Muthukumar_5
Honored Contributor

Re: Search pattern from a file

We can do this with sed / perl there. awk will need more programming there.

Simply with sed as,
$ cat file.1
test for now abc : def okie
bye for now
test abc : def over
$ sed -e '/abc : def/!d;s/.*\(abc : def\).*/\1/' file.1
abc : def
abc : def

It will check /abc : def/ there and delete others and try to print only that pattern in the selected line.
Easy to suggest when don't know about the problem!
Bharat Katkar
Honored Contributor

Re: Search pattern from a file

Rahul,
if there is only one colon per line then this should also work.
# export patt="abc:"
# cat filename | grep $patt | cut -d ":" -f2

This will display all the words followed by pattern:

Hope that helps.
Regards,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor
Solution

Re: Search pattern from a file


We can do with awk program as,

Is your pattern abc: then,
awk '{ for ( i=1; i<=NF; i++ ) if ( $i == "abc" && $(i+1) == ":" ) print $i" "$(i+1)" "$(i+2) }'

If abc: .. then,
awk '{ for ( i=1; i<=NF; i++ ) if ( $i == "abc:" ) print $i" "$(i+1) }'


Easy to suggest when don't know about the problem!
Anand_30
Regular Advisor

Re: Search pattern from a file

Hi MuthuKumar,

I tried using your awk solution for the line in the attached file to get the pattern 'Status:40' but it did not return any values.

Can you please look into it.

Thanks,
Rahul

Hein van den Heuvel
Honored Contributor

Re: Search pattern from a file

Rahul,

[In the future you may want to startup you own topic, referecing back to the original topic]

The solution Muthukumar proposes hinges on a space seperating the search target from the value target. Your file does nto have a space. We can tweak the solution to allow for both spaces and colons to be a seperator:

awk -F "[ :]" '{ for ( i=1; i<=NF; i++ ) if ( $i == "Status" ) print $i" "$(i+1) }' inputfile

Personally I find think my perl solution, just using a regular expression is more elegant:

perl -ne 'print "$1\n" if (/(Status:\w+)/)' inputfile


If neither of these suggestion solve your problem, then you need to open your own topic and explain exactly (in words) what the search pattern looks. For example: "the word "Status", followed immediatly with a colon and a numeric value".

Cheers,
Hein.
Muthukumar_5
Honored Contributor

Re: Search pattern from a file

Rahul,

We can use hein's method of perl or simply as,

$ awk '{ for (i=1;i<=NF;i++) if ($i=="Status:40") print $i }' anandh
Status:40

Your input file need not be separated with Field separator : there.

If you want to do with file separator : then,

$ awk -F "[ :]" '{ for ( i=1; i<=NF; i++ ) if ( $i == "Status" && $(i+1) == 40 ) print $i":"$(i+1) }'

Status:40

You can this users question and answers to know about awk programming as,
http://forums1.itrc.hp.com/service/forums/publicProfile.do?userId=CA1196931&forumId=1

HTH.

Easy to suggest when don't know about the problem!
Rahul_13
Advisor

Re: Search pattern from a file

Hi,

Both the solution works, but I have a new requirement. I have a file with multiple lines and I need to search for the pattern in the file. Display the pattern for whichever lines the pattern is found and display a blank for the lines where the pattern a not located. For example:

If the input is

..............Status:30.................
..............Status:40...................
..........................................
............................................
..............Status:80.....................

The output should be:

Status:30
Status:40
blank
blank
Status:80

Can you please help me.

Thanks,
Rahul