简体   繁体   English

我想从 file1 中的第 1 列和第 2 列中找到一些与 file2 中的第 1 列匹配的字符串/单词,并替换为 file2 中的第 2 列字符串/单词

[英]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.

相关问题 如果file1的B列= file2的B列,则将A列file1替换为file2的A列 - If column B of file1 = column B of file2, replace column A file1 with column A of file2 如何根据与file2的列匹配删除file1中的行 - How to delete lines in file1 based on column match with file2 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 Perl 使用 file2 从 file1 中删除单词 - Perl removing words from file1 with file2 当 ID 与 file2 匹配时,从 file1 复制一列,并根据文件 2 打印 output - copy a column from file1 when the ID's matches to file2 and print output according to file 2 搜索file1中包含的字符串中未找到的字符串 - Searching for strings contained in file1 that are NOT FOUND in file2 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件? - How to append each column of file1 to a specific field of file2 and make a new output file? 可能是grep,但仍然无法获得如何读取file1中的行并将其粘贴为file2中的列的方法 - Probably grep, but still do not get how to read row in file1 and paste it as a column in file2 如何使用awk删除Ubuntu的file2中存在列1值的file1行? - How to use awk to delete lines of file1 whose column 1 values exist in file2 in Ubuntu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM