简体   繁体   English

AWK如何打印多个信息并将所有信息重定向到文本文件

[英]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 在此方面需要帮助,我正在创建一个脚本,该脚本将分析文件,并希望使用Awk在输出txt文件中打印2条信息。我能够打印该信息没有人在我的屏幕上寻找内容,但是如何使用同一Awk打印另一个信息(例如,我分析的文件的行数),并将这两个信息输出到我使用此代码和erreor代码尝试过的文件teste.txt中:操作员期望

#!/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 . 尝试转义最后一行的引号,因此test.txt实际上在传递给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. 在执行END规则时,Awk变量NR最终将保持输入中的行数。 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. 请注意$outfile中没有任何有趣的字符。
  2. Unlike in shell, it's perfectly safe to use > both times. 不同于壳,这是完全可以放心使用>两次。 See here. 看这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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