- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Search pattern from a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 11:18 AM
тАО09-24-2004 11:18 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 11:25 AM
тАО09-24-2004 11:25 AM
Re: Search pattern from a file
grep " abc :" inputfile | sed 's/.*\( abc :\)\( .* \).*/\1\2/'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 02:30 PM
тАО09-24-2004 02:30 PM
Re: Search pattern from a file
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.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 06:07 PM
тАО09-24-2004 06:07 PM
Re: Search pattern from a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 08:05 PM
тАО09-24-2004 08:05 PM
Re: Search pattern from a file
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-24-2004 10:21 PM
тАО09-24-2004 10:21 PM
SolutionWe can do with awk program as,
Is your pattern abc
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) }'
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 10:28 AM
тАО10-21-2004 10:28 AM
Re: Search pattern from a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 01:54 PM
тАО10-21-2004 01:54 PM
Re: Search pattern from a file
[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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 06:17 PM
тАО10-21-2004 06:17 PM
Re: Search pattern from a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-22-2004 10:21 AM
тАО10-22-2004 10:21 AM
Re: Search pattern from a file
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