[英]I want to find some strings/words from column 1 and 2 in file1 that match column 1 in file2 and replace with column 2 strings/words in file2
I'm still learning coding using Linux platform.我仍在学习使用 Linux 平台进行编码。 I have search for problems similar to mine but the once I found they were either specific or focusing only on changing the entire column 1.我已经搜索了与我类似的问题,但是一旦我发现它们要么是特定的,要么只专注于更改整个第 1 列。
Here are example of my files:以下是我的文件示例:
File 1文件 1
abc Gamma 3.44
bcd abc 5.77
abc Alpha 1.99
beta abc 0.88
bcd Alpha 5.66
File 2文件 2
Gamma Bacteria
Alpha Bacteria
Beta Bacteria
Output file3 Output 文件3
abc Bacteria 3.44
bcd abc 5.77
abc Bacteria 1.99
Bacteria abc 0.88
bcd Bacteria 5.66
I have tried: awk:我试过:awk:
$ awk 'FNR==NR{a[$1]=$2;next} {if ($1,$2 in a){$1,$2=a[$1,$2]}; print $0}' file2 file1
$ awk 'NR==FNR {a[FNR]=$0; next} /$1|$2/ {$1 $2=a[FNR]} 1' file2 file1
They gave me:他们给了我:
abc Gamma 3.44
abc 5.77
abc Alpha 1.99
Bacteria abc 0.88
bcd Alpha 5.66
Only changing the $1 and remove the other text strings in column 1 which are not found in file2 $2仅更改 $1 并删除列 1 中未在 file2 $2 中找到的其他文本字符串
And this one:和这个:
$ awk -F'\t' -v OFS='\t' 'FNR==1 { next }FNR == NR { file2[$1,$2] = $1 FS $2 } FNR != NR { file1[$1,$2,] = $1 FS $2} END { print "Match:"; for (k in file1) if (k in file1) print file2[k] # Or file1[k]}' file2 file1
Didn't work没用
Then after i tried sed:然后在我尝试了 sed 之后:
$ sed = file2 | sed -r 'N;s/(.*)\n(.*)/\1s|\&$|\2|/' | sed -f - file1
This gave me an error and complained about sed -e not being called properly.这给了我一个错误并抱怨 sed -e 没有被正确调用。
Then after take only the smallest $3 if $1 and $2 or $2 and $1 are similar然后如果 $1 和 $2 或 $2 和 $1 相似,则只取最小的 $3
file 4文件 4
bcd abc 5.77
Bacteria abc 0.88
bcd Bacteria 5.66
I have tried this code:我试过这段代码:
$ awk 'NR == $1&$2 || $3 < min {line = $0; min = $3}END{print line}' file3
$ awk '/^$1/{if(h){print h RS m}min=""; h=$0; next}min=="" || $3 < min{min=$3; m=$0}END{print h RS m}' file3
$ awk -F'\t' '$3 != "NF==min"' OFS='\t' file3
$ awk -v a=NODE '{c=a*$3+(1-a)} !($1 in min) || c<min[$1]{min[$1]=c; minLine[$1]=$0} END{for(k in minLine) print minLine[k]}' file3 | column -t
All didn't work and i tried to research what what does each line means and changed it to fit my problem.一切都没有奏效,我试图研究每条线的含义并改变它以适应我的问题。 But they all failed但他们都失败了
This might work for you (GNU sed):这可能对您有用(GNU sed):
sed -E 's#(.*) (.*)#/^\1 /Is/\\S+/\2/;/^\\S+ \1 /Is/\\S+/\2/2#' file2 |
sed -Ef - file1
Generate a sed script from file2 which is run against file1 to produce the required format.从 file2 生成 sed 脚本,该脚本针对 file1 运行以生成所需的格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.