在AWK中组合多个文件和列

时间:2014-05-24 06:05:11

标签: bash awk

所以我有2个文件,一个人列表,一个人列表和一些描述。 我试图将这两个文件与AWK结合起来,但我从来没有在一个以上的文件上使用AWK,我只是想不通。

也许AWK不是最简单的方法,但我只是假设它是。

档案1

5 7/5/93 Steve
21 21/1/90 Bob
52 1/1/89 dale
21 21/1/90 Bob
52 1/1/89 dale

文件2

dale - is a cool guy
Steve - works at cosco
dale - is a cool guy
Steve - works at cosco
Steve - works at cosco

输出

5 7/5/93 Steve - works at cosco
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy

2 个答案:

答案 0 :(得分:2)

你走了:

awk -F" - " 'FNR==NR {a[$1]=$2;next} {split($0,b," ");print $0 (a[b[3]]?FS a[b[3]]:"")}' file2 file1
5 7/5/93 Steve - works at home
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy

awk非常适合根据不同的标准加入文件。

file 2中,您会为同一个人重复数据。这很好。如果同一个人有不同的数据,awk将使用它找到的最后一个并忽略所有其他数据。


另一种变化:

awk 'FNR==NR {sub(/ /,"| ");split($0,f,"|");a[f[1]]=f[2];next} {print $0 a[$3]}' file2 file1
5 7/5/93 Steve - works at home
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy
21 21/1/90 Bob
52 1/1/89 dale - is a cool guy

以下是它的工作原理:

awk '
FNR==NR {               # Run this section for the first file in the list (file2)
    sub(/ /,"| ")       # Change first space to "| " so we can split username from data
    split($0,f,"|")     # Split the sting in to "f" array devided by "|"
    a[f[1]]=f[2]        # Store data into array "a" using username as index
    next}               # Skip the next record.
    {                   # Run this section for file1
    print $0 a[$3]}     # Print all data from file1 and the data from array "a" (the user information)
    ' file2 file1       # Read the two files.

答案 1 :(得分:0)

你可以使用Bash:

while read line; do
    while read name trait; do
        if [[ $line == *"$name" ]]; then
            line="$line $trait"
            break
        fi
    done < file2
    echo $line
done < file1