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