Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将参数传递给do循环内的awk_Linux_Bash_Shell_Awk - Fatal编程技术网

Linux 将参数传递给do循环内的awk

Linux 将参数传递给do循环内的awk,linux,bash,shell,awk,Linux,Bash,Shell,Awk,我有大量以制表符分隔的文本文件,其中包含我感兴趣的第二列分数: test\u score\u 1.txt Title FRED Chemgauss4 File 24937 -6.111582 A 24972 -7.644171 A 26246 -8.551361 A 21453 -7.291059 A Title FRED Chemgauss4 File 14721 -7.322331 B 27280 -6.229842 B 21451

我有大量以制表符分隔的文本文件,其中包含我感兴趣的第二列分数:

test\u score\u 1.txt

Title   FRED Chemgauss4 File
24937   -6.111582   A
24972   -7.644171   A
26246   -8.551361   A
21453   -7.291059   A
Title   FRED Chemgauss4 File
14721   -7.322331   B
27280   -6.229842   B
21451   -8.407396   B
10035   -7.482369   B
10037   -7.706176   B
test\u score\u 2.txt

Title   FRED Chemgauss4 File
24937   -6.111582   A
24972   -7.644171   A
26246   -8.551361   A
21453   -7.291059   A
Title   FRED Chemgauss4 File
14721   -7.322331   B
27280   -6.229842   B
21451   -8.407396   B
10035   -7.482369   B
10037   -7.706176   B
我想检查我是否有分数小于我定义的数字的标题

下面的代码定义了我在脚本中的分数并起作用:

检查分数\u 1

#!/bin/bash

find . -name 'test_score_*.txt' -type f -print0 |
while read -r -d $'\0' x; do
    awk '{FS = "\t" ; if ($2 < -7.5) print $0}' "$x"
done
最后,
check_scores_3.sh
显示我实际上没有从命令行传递任何参数

检查分数\u 3.sh

#!/bin/bash

find . -name 'test_score_*.txt' -type f -print0 |
while read -r -d $'\0' x; do
    awk '{FS = "\t" ; if ($2 < ARGV[1]) print $0}' "$x"
done
#!/bin/bash

find . -name 'test_score_*.txt' -type f -print0 |
while read -r -d $'\0' x; do
    awk '{print ARGV[0] "\t" ARGV[1] "\t" ARGV[2]}' "$x"
done
$。/check\u score\u 3.sh“-7.5”
给出以下输出:

awk ./test_score_1.txt  
awk ./test_score_1.txt  
awk ./test_score_1.txt  
awk ./test_score_1.txt  
awk ./test_score_1.txt  
awk ./test_score_2.txt  
awk ./test_score_2.txt  
awk ./test_score_2.txt  
awk ./test_score_2.txt  
awk ./test_score_2.txt  
awk ./test_score_2.txt  

我做错了什么?

在shell脚本中,shell脚本的第一个参数是
$1
。您可以按如下方式将该值指定给awk变量:

find . -name 'test_score_*.txt' -type f -exec awk -v a="$1" -F'\t' '$2 < a' {} +
find-名称'test_score.*.txt'-type f-exec awk-va=“$1”-f'\t''$2
讨论
  • 您的print0/while读取循环非常好。但是,
    find
    提供的
    -exec
    选项使运行同一命令而无需任何显式循环成为可能

  • 可以选择将命令
    {if($2<-7.5)print$0}
    简化为条件
    $2<-7.5
    。这是因为条件的默认操作是
    print$0

  • 请注意,引用
    $1
    $2
    彼此完全无关。因为
    $1
    在双引号中,所以在awk命令开始运行之前,shell会将其替换为in。shell将
    $1
    解释为脚本的第一个参数。因为
    $2
    出现在单引号中,所以shell不使用它,而是由awk进行解释。Awk将其解释为其当前记录的第二个字段

    • 您的第一个示例:

      awk '{FS = "\t" ; if ($2 < -7.5) print $0}' "$x"
      
      awk'{FS=“\t”;如果($2<-7.5)打印$0}'”$x“
      
      这仅仅是一个令人高兴的巧合,设置FS实际上对您的特定情况没有影响。否则,输入文件的第一行将失败,因为在读取第一行并将其拆分为字段之后才设置FS。你的意思是:

      awk 'BEGIN{FS = "\t"} {if ($2 < -7.5) print $0}' "$x"
      
      awk'BEGIN{FS=“\t”}{if($2<-7.5)print$0}'“$x”
      
      可以用更地道的方式来写,如下所示:

      awk -F'\t' '$2 < -7.5' "$x"
      
      awk-F'\t'$2<-7.5'$x“
      
      对于第二种情况,正如你已经意识到的,你只是没有传递论点。您需要做的只是:

      awk -F'\t' -v max="$1" '$2 < max' "$x"
      
      awk-F'\t'-v max=“$1”'$2