Search term and output term in desired field

Hi All,

I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ?

Input_file:
aaa bbb ccc
ddd eee fff
hhhh pppp kkk jjjj
vvvv mm sss zzz

Output:
fff

This one!

#! /opt/third-party/bin/perl

my(@split_arr);

open(FILE, "< file") || die "Unable to open file <$!>\n";

while(<FILE>) {
  @split_arr = split(/ /, $_);
  if( $split_arr[0] =~ m/ddd/ ) {
    print "$split_arr[2]";
  }
}

close(FILE);
exit 0

Hi
this command would work try this
sed -n '\ddd\p' filename | awk '{print $3}'
you can do the same without sed and only awk

Best Regards,
Rakesh UV

since OP had asked for the reply in perl, i gave it that way

anyway here is with awk

awk '/^pattern/ { print $3 }' file
sed -n "/ddd/s/\([^ ]*\) \([^ ]*\) \([^ ]*\).*/\3/p" file
perl -lane 'if (/ddd/) { print $F[2] } ' file

Hi Anbu,

Your perl script works. But how can i make it in a form where i can place it in the format below (known as test.pl) below where i can just type perl test.pl, the ouput will be out.

#!/usr/bin/perl 

#contains some other codes as well

perl -lane 'if (/ddd/) { print $F[2] } ' file

That is one line code to perform your work.If you want to add some code add it here

test.pl
####################################
perl -lane 'if (/ddd/) { print $F[2] }; #your code ' file
####################################

Run the code just by typing the script name.

i named my script as testing.pl. But i am still seeing errors, pls advise.

perl -lane 'if (/ddd/) { print $F[2] }' textfile

Ran the code by typing testing.pl. Error encountered below
$ testing.pl
bash: testing.pl: command not found

Ran the code by typing perl testing.pl. Error encountered below.
$ perl testing.pl
String found where operator expected at testing.pl line 2, near "lane 'if (/ddd/
) { print $F[2] }'"
(Do you need to predeclare lane?)
Bareword found where operator expected at testing.pl line 2, near "'if (/ddd/) {
print $F[2] }' textfile"
(Missing operator before textfile?)
syntax error at testing.pl line 2, near "lane 'if (/ddd/) { print $F[2] }'"
Execution of testing.pl aborted due to compilation errors.

Try to run like this

./testing.pl

Make sure testing.pl is in your $PATH

Ran the code by typing perl testing.pl. Error encountered below.
$ perl testing.pl
String found where operator expected at testing.pl line 2, near "lane 'if (/ddd/
) { print $F[2] }'"
(Do you need to predeclare lane?)
Bareword found where operator expected at testing.pl line 2, near "'if (/ddd/) {
print $F[2] }' textfile"
(Missing operator before textfile?)
syntax error at testing.pl line 2, near "lane 'if (/ddd/) { print $F[2] }'"
Execution of testing.pl aborted due to compilation errors.

This should run perfectly
else

run a compilation check first

perl -c testing.pl

Hi Anbu,

Yours works by typing ./testing.pl but when i incorporate my code inside,it doesn;t seem to work. Why is this so ?

#!/usr/bin/perl

use strict;

$_ = 'NAME IS NOY. HOUSE IN CONE.MY HOUSE IS FILLED WITH JOY';
print "Enter some text to find: ";
my $pattern = <STDIN>;
chomp($pattern); 
print "\n\n";

open(FILE,'<','xxx_sorts') or die $!;
my $lineno;
while(<FILE>) {
print $lineno++;
print ": $_";
}
close FILE;

./testing.pl

On top to that, how can i introduce a global variable to store the value of the output of testing.pl

As i told you earlier, it is one liner. As i indicated earlier , you can add code to that to perform your additional task.
test.pl

perl -lane 'if (/'$1'/) { print $..":".$F[2] } ' file

$. contains current line number for the last filehandle accessed

Run like this

test.pl pattern

Hi Anbu,

Thanks for your explanation and your help.

Hi Matrixmadhan,

I think your code would be more suitable for me. But this statement@split_arr = split(/ /, $_);, can you modify such that it will not care about the number of spaces in between the fields,and as long as there's a space between 2 terms,it wil split.
EG:

Input_file:
aaa bbb ccc
ddd eee fff #notice the number of spaces before "fff" increases
hhhh pppp kkk jjjj
vvvv mm sss zzz
Output:
fff

then need to modify,

as

print "$split_arr[$#split_arr]";

Question:

I think anbu23's solution is straight and easier too, I am not sure why do you say its not working. But choice is always yours, just a thought :slight_smile:

Hi Matrix,

Your code only works if the desired value is the last term of a row where ddd exist.
If the number of spaces is random between every term, is there any thing we can do to change the condition of spilt portion -> @split_arr = split(/ /, $_);. Any thing to indicate random num of spacing as 1 space ?

And in perl, can we add in some nawk fucntions?
eg nawk -f xxx_awk file

A kind of solution was given based on your input.

For the search pattern 'ddd' your request was to print 'fff' which is the third term in the line.

Since there could be any number of spaces in between terms, it is necessary to extract only the last term in the line as a pattern.

Hence the recent solution.

It would be easier if you could post some more examples.

Sorry with that! :mad:

Try this

@split_arr = split(/ +/, $_);

Anbu, You are a Genius !!! That works just fine !!!
Thanks a million to both anbu and matrixmadhan!!

Hi Anbu,

I just found out that when there are spaces in front of "ddd", the code won;t work. Any ideas to overcome this problem ?

Input_file:
aaa bbb ccc
[space]ddd eee fff
hhhh pppp kkk jjjj
vvvv mm sss zzz