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