Operating System - Linux
1753846 Members
7643 Online
108807 Solutions
New Discussion юеВ

Extracting field using perl

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Extracting field using perl

Hi guys,

I will like to get the field from a line of string, using perl.
say the string is this:
"Process_name core kernel " and i will like to extract "core kernel from it" what is the best way to do this? Thanks in advance.

Best regards
Henry
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Extracting field using perl

Hi Henry:

# echo "Process_name core kernel"|perl -nle 'print $1 if m/(core kernel)/'

Regards!

...JRF...
Henry Chua
Super Advisor

Re: Extracting field using perl

Thanks James,

But what if I want to print the field after the first field i.e. for:

Process_name data 1

This will print "data 1"

Thanks.

Best regards
Henry
James R. Ferguson
Acclaimed Contributor
Solution

Re: Extracting field using perl

Hi (again) Henry:

OK, then:

# echo "Process_namedata 1"|perl -nle 'print $2 if m/(Process_name)\s+(.+)/'

data 1

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Extracting field using perl


If 'Process_name' has to be the first field on the line, then I would recommend indicating that with the BOL 'anchor' : "^"

That make the match quicker, and prevents false matched in case the string appears somewhere else. No need to save 'process_name' in $1 through () either.
So that would give:

echo "Process_name data 1"|perl -ne 'print $1 if m/^Process_name\s+(.+)/'

Hein.
Henry Chua
Super Advisor

Re: Extracting field using perl

Hi guys,

If I am reading lines from a file using perl.. how does the above applies to this?

Regards
Henry
Hein van den Heuvel
Honored Contributor

Re: Extracting field using perl


We used the 'echo' for easy one-line demo.

With a file you probably also need to
- loop
- remove the new-line
- decide whether you want a first occurence, a last or all.

For example:

perl -ne 'chomp; if (/^Process_name\s+(.+)/) {print $1; last}'

The -n requests perl to create a loop, Not printing each line. -p will loop and Print.

The chomp is not needed for a simple display, but probably is if you are sticking the result in a variable.

Hein.


James R. Ferguson
Acclaimed Contributor

Re: Extracting field using perl

Hi Henry:

To add to Hein's explanation a bit, consider this.

Using the '-n' or '-p' switch tells Perl to create a loop around your code, here, the argument of the '-e' switch enclosed in single quotes. Thus, instead of:

# perl -nle 'chomp;print $1 if m/^Process_name\s+(.+)/' filename

I could have written:

# cat .findpat
#/usr/bin/perl
while (<>) {
chomp;
print "$1\n" if if m/^Process_name\s+(.+)/'
}

# ./findpat filename

I used the '-l' switch on the commandline to cause print statements to have a newline character appended automatically to the output.

The read using "<>" is Perl's way of saying open the filename found in $ARGV and read it. If no @ARGV arguments are supplied, STDIN is used. Thus, using 'echo" piped to our Perl program is merely our Perl program reading STDIN. In every case, the read loop runs until there is no more input. A one-line 'echo' piped to our program is nothing more than a read of a file with one line.

If you want to read three (3) lines with a piped 'echo' you could do something as degenerate as:

# echo "line1\nline2\nline3"| perl -ple 'chomp'

Regards!

...JRF...