Operating System - Linux
1827243 Members
2319 Online
109716 Solutions
New Discussion

grep like functionality in Perl - what are my options?

 
SOLVED
Go to solution
Daavid Turnbull
Frequent Advisor

grep like functionality in Perl - what are my options?

I need to parse some field from a file. I can use back tick and grep to pick off the line I want and parse it with split which works but I figger there will be better ways to do it in Perl. (They say "There is more than one way to do it!")

(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.
Behold the turtle for he makes not progress unless he pokes his head out.
6 REPLIES 6
Muthukumar_5
Honored Contributor

Re: grep like functionality in Perl - what are my options?

You can use perl as,

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.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: grep like functionality in Perl - what are my options?

You can use awk as,

# 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 }' | tr -d '[]'

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: grep like functionality in Perl - what are my options?

You can as well as use sed to this as,

# sed -e '/:Key/!d;{s/^.*\[//;s/\]$//g;!d;}'

or

# sed -e '/:Key/!d;{s/^.*\[\(.*\)\]$/\1/g;!d;}'

hth.
Easy to suggest when don't know about the problem!
Daavid Turnbull
Frequent Advisor

Re: grep like functionality in Perl - what are my options?

The Perl answer is very neat and works well. (Though I am still groping my camel book to understand it.)

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.)

Behold the turtle for he makes not progress unless he pokes his head out.
Hein van den Heuvel
Honored Contributor
Solution

Re: grep like functionality in Perl - what are my options?


One 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.
Daavid Turnbull
Frequent Advisor

Re: grep like functionality in Perl - what are my options?

Thanks guys

I much appreciate your input.
Behold the turtle for he makes not progress unless he pokes his head out.