简体   繁体   中英

AWK how to print more than one information and redirect all to a text file

Need help on this I am creatinga script that will analyse a file and want to use Awk to print 2 informations in an output txt file I am able to print the information No one am looking for in my screen but how to print with the same Awk another information (exemple the number of lines of my file analyzed) and output those two information in a file calle test.txt I tried with this code and code erreor : operator expected

#!/usr/bin/perl

if ($#ARGV ==-1)
{
    print "Saisissez un nom de fichier a nalyser \n";
}
else
{
    $fname = $ARGV[0];
    open(FILE, $fname) || die ("cant open \n");
}
while($ligne=<FILE>)
{
    chop ($ligne);
    my ($elemnt1, $ellement2, $element3) = split (/ /, $ligne_);
}
system("awk '{print \$2 > "test.txt"}' $fname");

Try escaping the quotes on your last line, so test.txt is actually in the string passed to system .

system("awk '{print \$2 > \"test.txt\"}' $fname");

Edit: Adding the number of lines to the same file

The Awk variable NR ends up holding the number of lines in the input while the END rule is executing. Try this:

$outfile = '"test.txt"';
system("awk '{print \$2 > $outfile} END {print NR > $outfile}' $fname");

Notes:

  1. Watch out that $outfile doesn't have any funny characters in the name.
  2. Unlike in shell, it's perfectly safe to use > both times. See here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM