简体   繁体   English

使用sed和grep在file1中查找标题,并将内容移动到具有相同标题的file2

[英]use sed and grep to find title in file1 and move the content to file2 with same title

i have file1 and file2 我有file1和file2

FILE1 :
title1 linkabc
title2 linkdef
title3 linkfgh
title4 linkdse
title5 linklsfr
title6 linkfhghg
title7 linksdrlk
title8 linklklghkj
title9 linkghftht

and in FILE2 在FILE2中

FILE2 :
title1
anothertitle2
title3
anothertitle4
title5
anothertitle6
title7
anothertitle8
title9

as you can see in file2 there are no links only the title match , and i want to make the output like below 正如你在file2中看到的,没有链接只有标题匹配,我想输出如下所示

OUTPUT :
title1 linkabc
anothertitle2
title3 linkfgh
anothertitle4
title5 linklsfr
anothertitle6
title7 linksdrlk
anothertitle8
title9 linkghftht

As you can see if file1 and file2 title doesnt match leave as it is 正如您所看到的那样,file1和file2标题不匹配

i can pull the link with this command 我可以用这个命令拉链接

cat FILE1 | grep "title1" | grep -oh link.*

i just cant figure out how to append output link to FILE2 . 我只是想弄清楚如何将输出链接附加到FILE2。

after search test and failed again and again , i gave up , hope someone can help me solve this. 经过搜索测试并一次又一次失败,我放弃了,希望有人可以帮我解决这个问题。

thank you! 谢谢!

*sorry edited my question was not clear im too confused already , my bad. *抱歉编辑我的问题不清楚我已经太困惑了,我的不好。

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


title1 linkabc
anothertitle2
title3 linkfgh
anothertitle4
title5 linklsfr
anothertitle6
title7 linksdrlk
anothertitle8
title9 linkghftht

There are a number of ways to achieve this, but a simple approach would be to take every line of FILE2 and then grep for this in FILE1 . 有很多方法可以实现这一点,但一个简单的方法是采用FILE2每一行,然后在FILE1为此进行grep

while read -r LINE; do if ! grep "^$LINE " FILE1; then echo $LINE; fi; done < FILE2

Or if you prefer, you can cat the file first: 或者,如果您愿意,可以先录制文件:

cat FILE2 | while read -r LINE; do if ! grep "^$LINE " FILE1; then echo $LINE; fi; done

This will then just echo the LINE it's reading from FILE2 if there's no match in FILE1. 如果在FILE1中没有匹配,那么它将回显它从FILE2读取的LINE。

Example proof: 示例证明:

rseaman@Ubuntu-PC:~/temp$ cat FILE1
title1 linkabc
title2 linkdef
title3 linkfgh
title4 linkdse
title5 linklsfr
title6 linkfhghg
title7 linksdrlk
title8 linklklghkj
title9 linkghftht
rseaman@Ubuntu-PC:~/temp$ cat FILE2
title1
anothertitle2
title3
anothertitle4
title5
anothertitle6
title7
anothertitle8
title9
rseaman@Ubuntu-PC:~/temp$ while read -r LINE; do if ! grep "^$LINE " FILE1; then echo $LINE; fi; done < FILE2
title1 linkabc
anothertitle2
title3 linkfgh
anothertitle4
title5 linklsfr
anothertitle6
title7 linksdrlk
anothertitle8
title9 linkghftht

Updating answer because you updated the question 更新答案,因为您更新了问题

(grep -F -w -f file2 file1) && (grep -v -F -w -f <(cut -d' ' -f1 file1) file2)

will do what you seek, however it scrambles the input order. 会做你想要的,但它会扰乱输入顺序。 At this stage it probably makes sense to go with a more granular solution 在这个阶段,使用更精细的解决方案可能是有意义的

bash-3.00$ (grep -F -w -f file2 file1) && (grep -v -F -w -f <(cut -d' ' -f1 file1) file2)
title1 linkabc
title3 linkfgh
title5 linklsfr
title7 linksdrlk
title9 linkghftht
anothertitle2
anothertitle4
anothertitle6
anothertitle8

grep -f file2 file1

This will perform a partial match taking the input from file 2 这将执行部分匹配,从文件2获取输入

Here are the results 结果如下

bash-3.00$ cat file1
title1 linkabc
title2 linkdef
title3 linkfgh
title4 linkdse
title5 linklsfr
title6 linkfhghg
title7 linksdrlk
title8 linklklghkj
title9 linkghftht
bash-3.00$ cat file2
title1
title3
title5
title7
title9
bash-3.00$ grep -f file2 file1
title1 linkabc
title3 linkfgh
title5 linklsfr
title7 linksdrlk
title9 linkghftht
bash-3.00$

Addressing Robert's query, if that were the case i would do grep -F -w -f file2 file1 解决Robert的查询,如果是这种情况我会做grep -F -w -f file2 file1

bash-3.00$ cat file1
title1 linkabc
title2 linkdef
title3 linkfgh
title4 linkdse
title5 linklsfr
title6 linkfhghg
title7 linksdrlk
title8 linklklghkj
title9 linkghftht
title10 linkxyxyz
bash-3.00$
bash-3.00$
bash-3.00$
bash-3.00$ cat file2
title1
title3
title5
title7
title9
bash-3.00$
bash-3.00$
bash-3.00$
bash-3.00$ grep -F -w -f file2 file1
title1 linkabc
title3 linkfgh
title5 linklsfr
title7 linksdrlk
title9 linkghftht
bash-3.00$

After the update of the question You can try with gnu join : 更新问题后您可以尝试使用gnu join:

join --nocheck-order -a 2 FILE1 FILE2

If you want to update FILE2 如果要更新FILE2

printf "$(join --nocheck-order -a 2 FILE1 FILE2)" >FILE2

This might work for you (GNU sed): 这可能适合你(GNU sed):

sed 's#\(.*\) .*#s/^\1$/&/#' file1 | sed -f - file2

Turn file1 into a sed script and run it against file2. 将file1转换为sed脚本并针对file2运行它。

The script matches the key in file2 and replaces that line with the contents of the matching line from file1. 该脚本与file2中的键匹配,并将该行替换为file1中匹配行的内容。

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

相关问题 sed用file1中的file3内容替换file2内容 - sed replace file2 content with file3 content in file1 shell:通过FILE2中的内容从FILE1中获取行 - shell: Get line from FILE1 by content in FILE2 bash-根据file2中的ID查找file1中的名称 - bash - Find names in file1 according to IDs in file2 awk查找并在匹配时将变量file2替换为file1 - awk find and replace variable file2 into file1 when matched 从文件 2 中的文件 1 中找到最近的点,shell 脚本 - Find nearest point from file1 in file2, shell skript 如何使用 sed 或 awk 将 file1 的一部分替换为 file2 的一部分,部分由行和列描述 - How to replace a portion of file1 by a portion of file2 uing sed or awk, portion delineated by row and column no 将文件 1 中的字符串与文件 2 中的字符串匹配 - Match string in file1 with string in file2 使用 sed 或任何其他命令将部分行从 file1 复制到 file2 中的特定位置 - copy a part of a line from file1 to specific place in file2 using sed or any other command 将File1与File2合并(继续从File1追加到File2,直到不再有行) - Merge File1 with File2 (keep appending from File1 to File2 until no more rows) 理解大括号对重定向的影响 (&quot;&gt; file1 &gt;&amp; file2&quot; vs. &quot;{ &gt; file1; } &gt;&amp; file2&quot;) - Understanding the effect of curly braces with respect to redirections ("> file1 >& file2" vs. "{ > file1; } >& file2")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM