使用AWK使用第二个文件中的3个值从1个文件中查找3个值

使用AWK使用第二个文件中的3个值从1个文件中查找3个值,awk,Awk,我有两个文件。(两个真实文件的长度为50-100行) 文件1包含由4个字段组成的记录。 名称原始阅读成绩;原始数学成绩;原始科学分数 文件2有由4个字段组成的记录(查找表) 原始分数;转换阅读;转化数学;转化科学 对于任何给定的原始分数转换,此文件可能包含重复条目 e、 例如,原始分数8和9都等于科学的转换分数50 我想创建一个由7个字段组成的输出文件: 名称原始阅读成绩;转换阅读;原始数学成绩;转化数学;原始科学分数;转化科学 在下面的例子中,对于Smith,分数的结果 3,7,4应为: 3-

我有两个文件。(两个真实文件的长度为50-100行)

文件1包含由4个字段组成的记录。 名称原始阅读成绩;原始数学成绩;原始科学分数

文件2有由4个字段组成的记录(查找表) 原始分数;转换阅读;转化数学;转化科学 对于任何给定的原始分数转换,此文件可能包含重复条目 e、 例如,原始分数8和9都等于科学的转换分数50

我想创建一个由7个字段组成的输出文件: 名称原始阅读成绩;转换阅读;原始数学成绩;转化数学;原始科学分数;转化科学

在下面的例子中,对于Smith,分数的结果 3,7,4应为: 3-5、7-5、4-15(为了便于阅读,我添加了空格、破折号和逗号)

样本文件1(名称和3个原始分数)

样本文件2(原始和3个转换分数)

所需输出文件(名称,然后交替3个原始分数和3个转换分数)

所以我想我想把文件2读入一个数组,然后读入文件1,使用数组查找转换后的分数,然后输出名称和3组原始分数和转换后的分数

这是AWK可以完成的任务,还是我应该去别处看看

谢谢

Jim

这应该可以:

awk -F';' -v OFS=";" 'NR==FNR{a[$1]=$0;next}
{
split(a[$2],b)
split(a[$3],c)
split(a[$4],d)
print $1,$2,b[2],$3,c[3],$4,d[4]}' file2 file1

我认为这应该做到:

awk 'BEGIN{OFS=FS=";"}NR==FNR{s[$1,1]=$2;s[$1,2]=$3;s[$1,3]=$4;next}{print $1,$2,s[$2,1],$3,s[$3,2],$4,s[$4,3]}' table people
请注意文件的反转

解释:

# Before processing any lines
BEGIN{ 
    # Set the input and output field separators
    OFS=FS=";"
}
# For the first file
NR==FNR { 
    # Record the mappings - $1 is the first field, $2 the second, etc.
    s[$1,1]=$2;
    s[$1,2]=$3;
    s[$1,3]=$4;
    # Skip to the next line. This is often used 
    # instead of putting the opposite condition 
    # on the rest of the blocks, or putting a big 
    # if/else in one block.
    next
}
# Every line that reaches here, i.e. the second file
{
    # Print the student's name followed by each score raw and mapped.
    print $1, $2, s[$2,1], $3, s[$3,2], $4, s[$4,3]
}
这应该起作用:

awk '
BEGIN{FS=OFS=";"}
NR==FNR{cr[$1]=$2;cm[$1]=$3;cs[$1]=$4;next}
{print $1,$2,cr[$2],$3,cm[$3],$4,cs[$4]}
' file2 file1
输出

Smith;3;5;7;5;4;15
Jones;8;18;2;2;9;50
Doe;1;1;9;7;4;15

很好的评论!祝大家好运。这很有效!谢谢你的解释。我应该能够修改类似的问题。吉米不明白第一行的-v。我调整了一下,效果还不错。我得仔细阅读一下split命令。谢谢。@user2574126它和其他的“
BEGIN{FS=OFS=“;”}
# Before processing any lines
BEGIN{ 
    # Set the input and output field separators
    OFS=FS=";"
}
# For the first file
NR==FNR { 
    # Record the mappings - $1 is the first field, $2 the second, etc.
    s[$1,1]=$2;
    s[$1,2]=$3;
    s[$1,3]=$4;
    # Skip to the next line. This is often used 
    # instead of putting the opposite condition 
    # on the rest of the blocks, or putting a big 
    # if/else in one block.
    next
}
# Every line that reaches here, i.e. the second file
{
    # Print the student's name followed by each score raw and mapped.
    print $1, $2, s[$2,1], $3, s[$3,2], $4, s[$4,3]
}
awk '
BEGIN{FS=OFS=";"}
NR==FNR{cr[$1]=$2;cm[$1]=$3;cs[$1]=$4;next}
{print $1,$2,cr[$2],$3,cm[$3],$4,cs[$4]}
' file2 file1
Smith;3;5;7;5;4;15
Jones;8;18;2;2;9;50
Doe;1;1;9;7;4;15