perl oneliner not works .pl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl oneliner not works .pl script
# 1  
Old 11-24-2010
perl oneliner not works .pl script

I am trying to take first 3 columns in a file which matches the word "abc", but i am getting the below error,
<error>
Global symbol "@F" requires explicit package name at ./new.pl
</error>

whereas when i give the below,grep abc /home/test/file.txt|perl -lane 'print \"$F[0] $F[1] $F[2]\" in unix prompt i am getting the result. When i use inside the perl script it fails. When i remove "use strict" it returns nothing.Please help me out


Code:
#!/usr/bin/perl
use strict;
use warnings;
print `grep abc /home/test/file.txt|perl -lane 'print \"$F[0] $F[1] $F[2]\"' > a.txt`;


Last edited by DukeNuke2; 11-30-2010 at 05:21 AM..
# 2  
Old 11-24-2010
Quote:
Originally Posted by anspks
I am trying to take first 3 columns in a file which matches the word "abc", but i am getting the below error,
<error>
Global symbol "@F" requires explicit package name at ./new.pl
</error>

whereas when i give the below,grep abc /home/test/file.txt|perl -lane 'print \"$F[0] $F[1] $F[2]\" in unix prompt i am getting the result. When i use inside the perl script it fails. When i remove "use strict" it returns nothing.Please help me out

<code>
#!/usr/bin/perl
use strict;
use warnings;
print `grep abc /home/test/file.txt|perl -lane 'print \"$F[0] $F[1] $F[2]\"' > a.txt`;
</code>
Why do you invoke a Perl one-liner from within a Perl program ??

Code:
#!/usr/bin/perl
use strict;
use warnings;
my $file = "/home/test/file.txt";
my @x;
open (F, "<", $file) or die "Can't open $file: $!";
while (<F>) {
  chomp;
  if (/abc/) {
    @x = split;
    print "$x[0] $x[1] $x[2]\n";
  }
}
close(F) or die "Can't close $file: $!";

tyler_durden
# 3  
Old 11-25-2010
Hi,

Another solution:

Code:
$ perl -lane 'print(@F[0..2]) if grep(/abc/, $_)' file.txt

Regards,
Birei
# 4  
Old 11-29-2010
Birei,
It works when i put this is unix prompt but its not working inside a perl script,


Code:
#!/usr/bin/perl
use strict;
use warnings;
print `perl -lane 'print(@F[0..2]) if grep(/abc/, $_)' /home/test/file.txt > a.txt`;;


<error>
Possible unintended interpolation of @F in string at ./new.pl line 17.
Global symbol "@F" requires explicit package name at ./new.pl line 17.
</error>

Reason i am putting in script is, i have to process like this for different columns also inside a main foreach loop. This loop goes line by line in that file.txt.
Please help me to make this one-liner to work inside the perl script. Thanks.

Regards,
Prabhu

Last edited by DukeNuke2; 11-30-2010 at 05:21 AM..
# 5  
Old 11-30-2010
Hi,

I think my last script has an error, it should be like this:
Code:
 $ perl -lane '$,=" "; print(@F[0..2]) if grep(/abc/, $_)' file.txt

Here you have the script, test it:
Code:
$ cat script.pl
#!/usr/bin/perl
use warnings;
use strict;

$,=" ";
while (<>) {
    chomp;
    my @fields = split;
    print(@fields[0..($#fields>2?2:$#fields)], "\n") if grep(/abc/, $_);
}
$ ./script.pl file.txt
$ ## (Output supressed).

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

2. Shell Programming and Scripting

Need to understand how the line in perl program works as shell commend

I have a file with two line, one is header, the other actual value: TYPCD|ETID2|ETID|LEG ID|PTYP|PTYP SUB|TRD STATUS|CXL REASON|CACT|CACTNM|ENCD|ENC D NM|TRDR|ASDT|TRDT|MTDT|STDT|LS|SECID|SECID TYP|SECNM|PAR|STCC|MARKET PRICE|DIS MARKET PRICE|MARKET PRICE CURRENCY|SRC OF SETTLEMENT... (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Shell Programming and Scripting

Perl error in batch command but works one at a time

In the below perl executes if one file is processed perfect. However, when multiple files are processed in batch which is preferred I get the below error that I can not seem to fix it as the '' necessary for the command to execute, but seem to only work for one -arg option. Thank you :). ... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

perl: Command works in terminal, but not in shell script

Hi, the following command works in the terminal no problem. samtools view -h rawlib.bam | perl -ne '{ @line = split( /\s+/ ); $match = 0; while( $line =~ /(\d+)M/g ) { $match = $match + $1 } if( $match >= 80 || $_ =~ /^\@/ ) { print $_ } }' | java -Xmx12G -jar... (8 Replies)
Discussion started by: jdilts
8 Replies

5. Shell Programming and Scripting

awk - oneliner vs multiple line .

Dear Awk Experts, I can write oneliner some time but find difficulty writing like a flow of a program in multiple lines, could you please help how to write awk program , like long awk programs, any help to start with, For example, How to write this in multiple line: ls -l | awk... (6 Replies)
Discussion started by: rveri
6 Replies

6. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

7. Shell Programming and Scripting

perl oneliner to cut the file

Hi I have a file say text.txt and has data as below. text.txt ------- /abc/def/tom/hanks /abc/def/al/pacino /def/dgg/matt/damon Now I have to cut the field 3 and field 4 treating / as delimiter and save in the same file. Below is the how the output should be in the same file. I... (1 Reply)
Discussion started by: lijjumathew
1 Replies

8. Shell Programming and Scripting

need a df -h oneliner..

can any one suggest some one liner such that when executed will display df -h output of the mount points that are 90% or more that it.. and not the remaining.. (7 Replies)
Discussion started by: chidori
7 Replies

9. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

10. Shell Programming and Scripting

perl - how come this script works?

#!/usr/bin/perl open (DATA, file.txt); @array = <DATA>; close (DATA); open (DATA, ">$file.txt"); for (@array) { s/text/replace text/; push(@contents,$_); } seek(DATA, 0, 0); print DATA (@contents); close(DATA); could someone please explain how this works. i've been... (3 Replies)
Discussion started by: mjays
3 Replies
Login or Register to Ask a Question