使用两个文件的Bash/AWK条件

使用两个文件的Bash/AWK条件,awk,conditional-statements,Awk,Conditional Statements,首先,谢谢你的帮助。我在尝试使用两个文件的bash条件时遇到问题。我有文件letters.txt A B C 我有文件号.txt B 10 D 20 A 15 C 18 E 23 A 12 B 14 我想使用条件,因此,如果文件letter.txt中的一个字母也在number.txt中,它将生成文件a.txt b.txt c.txt,因此如下所示: a、 文本 b、 文本 c、 文本 我知道我可以使用以下代码: cat number.txt | awk '{if($1==&qu

首先,谢谢你的帮助。我在尝试使用两个文件的bash条件时遇到问题。我有文件letters.txt

A
B
C
我有文件号.txt

B  10
D  20
A  15
C  18
E  23
A  12
B  14
我想使用条件,因此,如果文件letter.txt中的一个字母也在number.txt中,它将生成文件a.txt b.txt c.txt,因此如下所示:

a、 文本

b、 文本

c、 文本

我知道我可以使用以下代码:

cat number.txt | awk '{if($1=="A")print $0}' > a.txt

但是我想用两个文件来做这件事。

对于您显示的示例,请尝试以下内容

awk '
FNR==NR{
  arr[$0]
  next
}
($1 in arr){
  outputFile=(tolower($1)".txt")
  print >> (outputFile)
  close(outputFile)
}
' letters.txt numbers.txt
说明:添加上述内容的详细说明

awk '                      ##Starting awk program from here.
FNR==NR{                   ##Checking condition which will be TRUE when letters.txt is being read.
  arr[$0]                  ##Creating array arr with index of current line.
  next                     ##next will skip all further statements from here.
}
($1 in arr){               ##checking condition if 1st field is present in arr.
  outputFile=(tolower($1)".txt")    ##Creating outputFile to print output.
  print >> (outputFile)    ##Printing current line into output file.
  close(outputFile)        ##Closing output file in backend.
}
' letters.txt numbers.txt  ##Mentioning Input_file names here.

有了您展示的样品,请尝试以下内容

awk '
FNR==NR{
  arr[$0]
  next
}
($1 in arr){
  outputFile=(tolower($1)".txt")
  print >> (outputFile)
  close(outputFile)
}
' letters.txt numbers.txt
说明:添加上述内容的详细说明

awk '                      ##Starting awk program from here.
FNR==NR{                   ##Checking condition which will be TRUE when letters.txt is being read.
  arr[$0]                  ##Creating array arr with index of current line.
  next                     ##next will skip all further statements from here.
}
($1 in arr){               ##checking condition if 1st field is present in arr.
  outputFile=(tolower($1)".txt")    ##Creating outputFile to print output.
  print >> (outputFile)    ##Printing current line into output file.
  close(outputFile)        ##Closing output file in backend.
}
' letters.txt numbers.txt  ##Mentioning Input_file names here.

解决这类问题的有效方法是首先对键字段上的输入进行排序,这样就不需要同时打开多个输出文件(有限制和/或会降低处理速度),也不需要在每次读取行时打开/关闭输出文件(总是很慢)

使用GNU sort for
-s
(稳定排序)保留非关键字段的输入顺序,一次只打开一个输出文件,并在整个填充过程中保持打开状态:

$ sort -k1,1 -s number.txt |
awk '
    NR==FNR { lets[$1]; next }
    !($1 in lets) { next }
    $1 != prev { close(out); out=tolower($1) ".txt"; prev=$1 }
    { print > out }
' letters.txt -

$ head ?.txt
==> a.txt <==
A  15
A  12

==> b.txt <==
B  10
B  14

==> c.txt <==
C  18
请注意,
A
的第二个字段的顺序与输入顺序相比发生了变化,而无需执行上述操作,因为默认情况下,
排序
不保证为其排序的每个键保留相对行顺序:

$ sort -k1,1 number.txt
A  12
A  15
B  10
B  14
C  18
D  20
E  23

解决这类问题的有效方法是首先对键字段上的输入进行排序,这样就不需要同时打开多个输出文件(有限制和/或会降低处理速度),也不需要在每次读取行时打开/关闭输出文件(总是很慢)

使用GNU sort for
-s
(稳定排序)保留非关键字段的输入顺序,一次只打开一个输出文件,并在整个填充过程中保持打开状态:

$ sort -k1,1 -s number.txt |
awk '
    NR==FNR { lets[$1]; next }
    !($1 in lets) { next }
    $1 != prev { close(out); out=tolower($1) ".txt"; prev=$1 }
    { print > out }
' letters.txt -

$ head ?.txt
==> a.txt <==
A  15
A  12

==> b.txt <==
B  10
B  14

==> c.txt <==
C  18
请注意,
A
的第二个字段的顺序与输入顺序相比发生了变化,而无需执行上述操作,因为默认情况下,
排序
不保证为其排序的每个键保留相对行顺序:

$ sort -k1,1 number.txt
A  12
A  15
B  10
B  14
C  18
D  20
E  23
$ sort -k1,1 number.txt
A  12
A  15
B  10
B  14
C  18
D  20
E  23