Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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/2/shell/5.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
Bash Unix shell文件搜索值对_Bash_Shell_Unix_Awk - Fatal编程技术网

Bash Unix shell文件搜索值对

Bash Unix shell文件搜索值对,bash,shell,unix,awk,Bash,Shell,Unix,Awk,我正在对我们的CI进行客户提取,我需要一段数据,它以键、值格式存储在另一个文件中 例如: 文件1(摘录): 文件2(键、值): 我需要做的是在文件2中查找值PVXZT1000,并找到与之关联的值。然后,我需要将文件1中的123-3455值替换为文件2中的新值,136-8400 有没有一种简单而有效的方法可以使用unixshell实现这一点?或者也许是AWK 我可以使用任何常见的unix shell。您可以使用awk: awk 'NR==FNR{a[$1]=$2;next}{$5=a[$4]}1'

我正在对我们的CI进行客户提取,我需要一段数据,它以键、值格式存储在另一个文件中

例如:

文件1(摘录):

文件2(键、值):

我需要做的是在文件2中查找值
PVXZT1000
,并找到与之关联的值。然后,我需要将文件1中的
123-3455
值替换为文件2中的新值,
136-8400

有没有一种简单而有效的方法可以使用unixshell实现这一点?或者也许是AWK


我可以使用任何常见的unix shell。

您可以使用awk:

awk 'NR==FNR{a[$1]=$2;next}{$5=a[$4]}1' file2 file1

您可以使用awk执行此操作:

awk 'NR==FNR{a[$1]=$2;next}{$5=a[$4]}1' file2 file1

单向使用
awk

$ awk 'NR==FNR{a[$1]=$2;next}($4 in a){$5=a[$4]}1' file2 file1
1, 3000, 4000, PVXZT1000, 136-8400
2, 4000, 2500, BT21304, 136-8400
这将通过匹配文件中的键,使用
file2
中的最新值更新
file1

说明:

NR
是一个
awk
变量,包含当前正在读取的行号
FNR
包含当前文件上下文中的当前行,即每次我们读取新文件时,
FNR
重置为0,但
NR
不重置

          # Looking at file2 first
NR==FNR   # This is only true when we look at the first file
{         # The block to execute if the previous condition is TRUE
a[$1]=$2  # Create a look-up array a: field 1 is the key and field 2 the value
next      # Grab the next line (skips executing any further blocks)
}         # End block
          # We are now looking a file1
($4 in a) # Check in field 4 was in file2
$5=a[$4]  # If found update field 5 with value in the array using the key
}         # End the block
1         # Idiom for printing all the lines 

如果您想学习
awk
请使用
awk

$ awk 'NR==FNR{a[$1]=$2;next}($4 in a){$5=a[$4]}1' file2 file1
1, 3000, 4000, PVXZT1000, 136-8400
2, 4000, 2500, BT21304, 136-8400
这将通过匹配文件中的键,使用
file2
中的最新值更新
file1

说明:

NR
是一个
awk
变量,包含当前正在读取的行号
FNR
包含当前文件上下文中的当前行,即每次我们读取新文件时,
FNR
重置为0,但
NR
不重置

          # Looking at file2 first
NR==FNR   # This is only true when we look at the first file
{         # The block to execute if the previous condition is TRUE
a[$1]=$2  # Create a look-up array a: field 1 is the key and field 2 the value
next      # Grab the next line (skips executing any further blocks)
}         # End block
          # We are now looking a file1
($4 in a) # Check in field 4 was in file2
$5=a[$4]  # If found update field 5 with value in the array using the key
}         # End the block
1         # Idiom for printing all the lines 

如果您想学习
awk
阅读

注意:这假设文件1中的每个值都将使用文件2中的有效值进行更新。@sudo\u是的,您是正确的。您的答案可能更可靠注意:这假设文件1中的每个值都将使用文件2中的有效值进行更新。@sudo\u O是的,您是正确的。您的答案可能更可靠如果在文件2中找不到密钥,您会如何处理文件1?清除值“123-3455”或保持该值不变?但是无论它应该遵循哪条规则,您都可以从两位awk专家那里找到答案。如果在文件2中找不到密钥,您会如何处理文件1?清除值“123-3455”或保持该值不变?但无论遵循哪条规则,您都可以从两位awk专家那里找到答案。谢谢您的回复。如果我想清除文件1中的值(如果在文件2中找不到密钥),我是否需要更改任何内容?还有,如果你有时间的话,你是否可以给我发一个PM,帮我把它分解一下?我想更多地理解awk。解释对所有人都有好处,所以我把它添加到了我的答案中。如果您想清除值,您将删除复选框
($4 in a)
,即@user000001 answer的作用,这就是为什么我添加了一个注释以清除差异。您运行的确切命令是什么?你使用的是什么系统?您要在Solaris上使用
/usr/xpg4/bin/awk
可以使用
awk--version | head-n 1
获得
awk版本。是的,这是非常有效的。感谢您的回复。如果我想清除文件1中的值(如果在文件2中找不到密钥),我是否需要更改任何内容?还有,如果你有时间的话,你是否可以给我发一个PM,帮我把它分解一下?我想更多地理解awk。解释对所有人都有好处,所以我把它添加到了我的答案中。如果您想清除值,您将删除复选框
($4 in a)
,即@user000001 answer的作用,这就是为什么我添加了一个注释以清除差异。您运行的确切命令是什么?你使用的是什么系统?您可以在Solaris上使用
/usr/xpg4/bin/awk
awk--version | head-n 1
获得
awk版本,这是完全有效的。