我有一个源文件,该文件具有2m 的文本,看起来像这样:
388708091|347|||||0010.60|N01/2012|
388708101|348|||||0011.60|N01/2012|
388708101|349|||||0012.60|N01/2012|
388719001|348|||||0010.38|M05/2013|
388719001|349|||||0011.38|M05/2013|
我想用图像下面的地图映射并替换第二列(具有347,348,349等值之类的值):
346 309
347 311
348 312
349 313
350 314
351 315
352 316
请注意,当地图为2-D时,有100行超过100行。
用目标映射替换源文件第二列中数据的最有效命令行的方法是什么?
awk
似乎是工作的工具:
awk 'NR == FNR { a[$1] = $2; next } FNR == 1 { FS = "|"; OFS = FS; $0 = $0 } { $2 = a[$2] } 1' mapfile datafile
代码工作如下:
NR == FNR { # while processing the first file (mapfile)
a[$1] = $2 # remember the second field by the first
next # do nothing else
}
FNR == 1 { # at the first line of the second file (datafile):
FS = "|" # start splitting by | instead of whitespace
OFS = FS # delimit output the same way as the input
$0 = $0 # force resplitting of this first line
}
{ # for all lines in the second file:
$2 = a[$2] # replace the 2nd field with the remembered value for that key
}
1 # print the line
警告:这假定数据文件的第二列中的每个值都在地图文件中具有相应的条目。那些不会用空字符串代替的人。如果这种行为不可取,请替换
{ $2 = a[$2] }
{ if($2 in a) { $2 = a[$2] } else { $2 = "something else" } }
对我而言,在这种情况下应该发生什么。