简体   繁体   English

最多将文件的一列与另一文件匹配,并使用bash替换不同的列

[英]Match up to on one column of file with another file and replace different column using bash

I'm a bit of a noob with this so forgive my ignorance if I am not clear enough. 我对此有些菜鸟,如果我不太清楚,请原谅我的无知。 I have two files. 我有两个文件。 File1 looks like below. File1如下所示。

file1

AA--:Aa-to-Bb:VGG sometext 1223344 10000 sometext
AB--:Aa-to-Bb:VGG sometext 1223344 12000 sometext
CC--:Dd-to-Ee:VGG sometext 1223344 44000 sometext
AA--:Zz-to-Yy:VGG sometext 1223344 51000 sometext
DD--:Aa-to-Dd:VGG sometext 1223344 36000 sometext

File2 looks similar but there are differences in columns 1 and 4. File2看起来相似,但第1列和第4列有所不同。

file2

CC--:Dd-to-Ee:VGG6645 sometext 1223344 3000 sometext
AA--:Zz-to-Yy:VGG2244 sometext 1223344 1000 sometext
AA--:Aa-to-Bb:VGG12345 sometext 1223344 12200 sometext
AB--:Aa-to-Bb:VGG76523 sometext 1223344 33300 sometext
EE--:Dd-to-Ee:VGG45123 sometext 1223344 76500 sometext
DD--:Aa-to-Dd:VGG3486 sometext 1223344 400 sometext
AA--:Dd-to-Ee:VGG4512 sometext 1223344 22300 sometext
AA--:Zz-to-Dd:VGG98765 sometext 1223344 7000 sometext
CC--:Dd-to-Zz:VGG43576 sometext 1223344 900 sometext
FF--:Zz-to-Yy:VGG5645 sometext 1223344 91200 sometext
CC--:Zz-to-Ee:VGG23456 sometext 1223344 3400 sometext
AA--:Ff-to-Yy:VGG1111 sometext 1223344 51000 sometext

I am trying to do some auto-processing via a bash script. 我正在尝试通过bash脚本进行一些自动处理。 One of the tasks is to compare the two files. 任务之一是比较两个文件。 If an entry in file1 column 1 matches an entry in file2 column 1 up to and including the "VGG", then the value on column 4 in file1 replaces the value in column 4 in file2. 如果file1列1中的条目与file2列1中的条目匹配,直到并包括“ VGG”,则file1中第4列的值将替换file2中第4列的值。

new file2

CC--:Dd-to-Ee:VGG6645 sometext 1223344 44000 sometext
AA--:Zz-to-Yy:VGG2244 sometext 1223344 51000 sometext
AA--:Aa-to-Bb:VGG12345 sometext 1223344 10000 sometext
AB--:Aa-to-Bb:VGG76523 sometext 1223344 12000 sometext
EE--:Dd-to-Ee:VGG45123 sometext 1223344 76500 sometext
DD--:Aa-to-Dd:VGG3486 sometext 1223344 36000 sometext
AA--:Dd-to-Ee:VGG4512 sometext 1223344 22300 sometext
AA--:Zz-to-Dd:VGG98765 sometext 1223344 7000 sometext
CC--:Dd-to-Zz:VGG43576 sometext 1223344 900 sometext
FF--:Zz-to-Yy:VGG5645 sometext 1223344 91200 sometext
CC--:Zz-to-Ee:VGG23456 sometext 1223344 3400 sometext
AA--:Ff-to-Yy:VGG1111 sometext 1223344 51000 sometext

I have tried using various awk and sed commands, as well as grep, sort, and others and I cannot get the results I am looking for. 我尝试使用各种awk和sed命令以及grep,sort和其他命令,但无法获得所需的结果。 Any help or suggestions you have would be greatly appreciated. 您的任何帮助或建议将不胜感激。

Below awk may help 低于awk可能会有所帮助

$ awk 'NR==FNR{rec[$1]=$4;next}{temp=$1;sub(/VGG[0-9]+$/,"VGG",temp)} temp in rec{$4=rec[temp]}1' file1 file2

Output 产量

CC--:Dd-to-Ee:VGG6645 sometext 1223344 44000 sometext
AA--:Zz-to-Yy:VGG2244 sometext 1223344 51000 sometext
AA--:Aa-to-Bb:VGG12345 sometext 1223344 10000 sometext
AB--:Aa-to-Bb:VGG76523 sometext 1223344 12000 sometext
EE--:Dd-to-Ee:VGG45123 sometext 1223344 76500 sometext
DD--:Aa-to-Dd:VGG3486 sometext 1223344 36000 sometext
AA--:Dd-to-Ee:VGG4512 sometext 1223344 22300 sometext
AA--:Zz-to-Dd:VGG98765 sometext 1223344 7000 sometext
CC--:Dd-to-Zz:VGG43576 sometext 1223344 900 sometext
FF--:Zz-to-Yy:VGG5645 sometext 1223344 91200 sometext
CC--:Zz-to-Ee:VGG23456 sometext 1223344 3400 sometext
AA--:Ff-to-Yy:VGG1111 sometext 1223344 51000 sometext

If you need to rewrite file2 如果需要重写file2

$ awk 'NR==FNR{rec[$1]=$4;next}
      {temp=$1;sub(/VGG[0-9]+$/,"VGG",temp)}
      temp in rec{$4=rec[temp]}1' file1 file2 >filetmp && mv filetmp file2

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed -r 's|^(\S+)(\s(\S+)){3}\s.*|/^\1/s/\\S+/\3/4|' file2 | sed -rf - -i file1

This makes a sed script from file2 and then runs it against file1. 这将从文件2中创建一个sed脚本,然后针对文件1运行它。

The sed script generated from file2, matches a line in the object file using the first column value and then replaces the fourth column in the object file with the value in the its fourth column. 从file2生成的sed脚本使用第一列的值匹配目标文件中的一行,然后用其第四列中的值替换目标文件中的第四列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Bash 使用另一个文本文件中存在的转换替换文本文件列中的名称 - How to Replace Names in Column of a Text File, Using a Conversion Present in Another Text File With Bash BASH使用从另一文件中的一列传递的值来递归制作堆积文件 - BASH Making pileup files recursively using values piped from one column in another file 使用另一个文件的引用替换特定的列值 - replace specific column values using reference of another file 如何在bash脚本中求和不同文件的列 - How to sum column of different file in bash scripting 替换一列bash中的模式 - Replace pattern in one column bash 如何使用 awk 将一个文件的一列与另一个文件的另一列进行比较? - How do I compare one column of a file with another column of another file using awk? 提取一列的一部分并使用awk保存到另一文件中 - Extract part of one column and save into another file using awk 如何在Linux中匹配2个文件,一个文件有1列,第二个文件有2列 - How to match 2 files in Linux one file with 1 column and the second file with 2 columns 如何使用 linux 中的连接命令将两个不同长度和不同列的文本文件与 header 匹配 - How to match two different length and different column text file with header using join command in linux 在分隔文件中将一列复制到另一列 - Copy one column over another in a delimited file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM