- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- grep like functionality in Perl - what are my opti...
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
Forums
Discussions
Discussions
Discussions
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
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
08-18-2005 03:53 PM
08-18-2005 03:53 PM
(I think my real problem is that I have been playing with shells for too many years and it is stopping me from getting into and efficient Perl mind set.)
Conceptually I can think of a number of approches and I am seeking some advice on their relative merits.
Consider a text file that looks a bit like this:
_____________
Line 1
Line 2
Line 3
Line of interest:Key [field of interest]
line 5
line 6
_____________
In ksh I would grep for (say) ":Key" and use awk to extract "field of interest". Basically this is using successive filters to extract the field of interest.
Using built in Perl functions I could sort of do the same thing, reading every line and checking it with regular expressions for ":Key" and use split to extract "field of interest" into a variable. To me this seems clumbsy and possibly inefficient.
I could also read the whole file, seek to ":Key [" and extract up to "]" into my variable or just split out the variable based on the patterns.
Producing readable code is probably more important than effientcy but Ideally I want both ;-)
Is there a "best" way to do this?
Are there other approaches that I might find useful.
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2005 06:28 PM
08-18-2005 06:28 PM
Re: grep like functionality in Perl - what are my options?
perl -ne '$,=" ";if ( /:Key/ ) { y/[]//d;@str=(split (/\s+/))[3..5];print @str; }'
$, = Output Field separator to " "
Remote [] in input line
Get 3,4,5 fields and store into an arrary @str
print @str
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2005 06:30 PM
08-18-2005 06:30 PM
Re: grep like functionality in Perl - what are my options?
# awk '/:Key/ { split($4,a,"[");split($6,b,"]");print a[2]" "$5" "b[1] }'
or
with awk + tr as,
# awk '/:Key/ { print $4" "$5" "$NF }'
hth.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2005 06:41 PM
08-18-2005 06:41 PM
Re: grep like functionality in Perl - what are my options?
# sed -e '/:Key/!d;{s/^.*\[//;s/\]$//g;!d;}'
or
# sed -e '/:Key/!d;{s/^.*\[\(.*\)\]$/\1/g;!d;}'
hth.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2005 01:02 PM
08-21-2005 01:02 PM
Re: grep like functionality in Perl - what are my options?
I have not looked at the awk and sed options. (This is a small part of a range of functionality required by the script the remainder of which has Perl written all over it - hence the specific request for Perl hints.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-21-2005 02:15 PM
08-21-2005 02:15 PM
SolutionOne of many other perl solutions, but perhaps more into that 'other mindset':
perl -ne 'print "$1\n" if (/Key \[(.*)\]/)' x
In this regexpr we look for ":Key [" to get going.
However, the [ needs to be escaped.
Then we start remembering: (
Anything: .*
Untill: )
We see a "]": \] (again escaped)
If this matches then print the first string remembered: $1
If the field of interest is everything after the ":Key " then the solution becomes:
perl -ne 'print "$1\n" if (/:Key\s+(.*)$/)' x
The \s+ represents any aount of whitespace.
hth,
Hein.
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2005 05:56 PM
08-22-2005 05:56 PM
Re: grep like functionality in Perl - what are my options?
I much appreciate your input.