使用文件2中与bash(可能是awk)匹配的密钥值将值更新到文件_1中

使用文件2中与bash(可能是awk)匹配的密钥值将值更新到文件_1中,awk,Awk,我有以下情况: 包含ddl的文件_1可以是这样的: column_A varchar(50) column_B int column_C varchar (100) 文件_2包含文件_1中更改的内容,例如 column_B varchar(50) 我想将文件_1从“column_B int”更改为“column_B varchar(50)” 当然,我还需要所有其他线路保持不变 使用awk,我成功地识别了更改的线路 awk 'NR==FNR{a[$1]++;next} ($1 in a

我有以下情况: 包含ddl的文件_1可以是这样的:

column_A  varchar(50)
column_B  int
column_C  varchar (100)
文件_2包含文件_1中更改的内容,例如

column_B  varchar(50)
我想将文件_1从“column_B int”更改为“column_B varchar(50)” 当然,我还需要所有其他线路保持不变

使用awk,我成功地识别了更改的线路

awk 'NR==FNR{a[$1]++;next} ($1 in a)' file_1 file_2
但我不知道如何将其写入文件_1


我该如何实现它呢?

请您尝试以下内容,并用GNU
awk
中显示的示例编写和测试

awk 'FNR==NR{a[$1]=$2;next} ($1 in a){$2=a[$1]} 1' file_2 file_1
若上面的命令正在终端上为您打印正确的结果,那个么您可以使用下面的命令将输出保存到输入文件中

awk 'NR==FNR{a[$1]++;next} ($1 in a)' file_1 file_2 > temp && mv temp file_1
说明:添加上述内容的详细说明

awk '               ##Starting awk program from here.
FNR==NR{            ##Checking condition if FNR==NR which will be true when first Input_file named file_2 is being read.
  a[$1]=$2          ##Creating array a with index of $1 and its value is $2 from current line.
  next              ##next will skip all further statements from here.
}
($1 in a){          ##Checking condition if first field is present in array a then do following.
  $2=a[$1]          ##Assigning array a value with index of first field to 2nd field.
}
1                   ##1 will print current line here.
' file_2 file_1     ##Mentioning Input_file names here.

请您尝试使用GNU
awk
中显示的样本编写并测试以下内容

awk 'FNR==NR{a[$1]=$2;next} ($1 in a){$2=a[$1]} 1' file_2 file_1
若上面的命令正在终端上为您打印正确的结果,那个么您可以使用下面的命令将输出保存到输入文件中

awk 'NR==FNR{a[$1]++;next} ($1 in a)' file_1 file_2 > temp && mv temp file_1
说明:添加上述内容的详细说明

awk '               ##Starting awk program from here.
FNR==NR{            ##Checking condition if FNR==NR which will be true when first Input_file named file_2 is being read.
  a[$1]=$2          ##Creating array a with index of $1 and its value is $2 from current line.
  next              ##next will skip all further statements from here.
}
($1 in a){          ##Checking condition if first field is present in array a then do following.
  $2=a[$1]          ##Assigning array a value with index of first field to 2nd field.
}
1                   ##1 will print current line here.
' file_2 file_1     ##Mentioning Input_file names here.