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

时间:2018-04-02 19:32:48

标签: bash awk sed grep

我有file1和file2

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

和FILE2

FILE2 :
title1
anothertitle2
title3
anothertitle4
title5
anothertitle6
title7
anothertitle8
title9

正如你在file2中看到的,没有链接只有标题匹配, 我想要输出如下

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

正如您所看到的那样,file1和file2标题与

是否匹配

我可以使用此命令拉出链接

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

我只是想弄清楚如何将输出链接附加到FILE2。

经过搜索测试并一次又一次失败,我放弃了,希望有人可以帮我解决这个问题。

谢谢你!

*抱歉编辑我的问题不清楚我已经太困惑了,我的不好。

5 个答案:

答案 0 :(得分:3)

runPromo

答案 1 :(得分:0)

有很多方法可以实现这一目标,但一个简单的方法是在FILE2中为此grep然后FILE1取每一行。

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

或者,如果您愿意,可以先录制文件:

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

如果FILE1中没有匹配项,那么它将回显它从FILE2读取的LINE。

示例证明:

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

答案 2 :(得分:0)

因为您更新了问题而更新了答案

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

会做你想要的,但它会扰乱输入顺序。在这个阶段,使用更精细的解决方案可能是有意义的

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

这将执行部分匹配,从文件2

获取输入

以下是结果

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$

解决罗伯特的问题,如果是这样,我会这样做 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$

答案 3 :(得分:0)

更新问题后您可以尝试使用gnu join:

join --nocheck-order -a 2 FILE1 FILE2

如果您想更新FILE2

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

答案 4 :(得分:0)

这可能适合你(GNU sed):

Crashed: com.apple.main-thread
0  _                          0x100967ba0 specialized TokensViewModel.parallelOperations(for : [TokenObject]) -> () (TokensViewModel.swift:73)
1  _                          0x100967dac specialized TokensViewModel.(runOperations() -> ()).(closure #1).(closure #1) (TokensViewModel.swift:151)
2  _                          0x1009681b0 partial apply for TokensViewModel.(runOperations() -> ()).(closure #1).(closure #1) + 4300276144
3  _                          0x10098fe90 thunk (FloatLabelTextField.swift)
4  libdispatch.dylib              0x184d9cb24 _dispatch_call_block_and_release + 24
5  libdispatch.dylib              0x184d9cae4 _dispatch_client_callout + 16
6  libdispatch.dylib              0x184da96e0 _dispatch_main_queue_callback_4CF$VARIANT$mp + 1012
7  CoreFoundation                 0x185453070 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
8  CoreFoundation                 0x185450bc8 __CFRunLoopRun + 2272
9  CoreFoundation                 0x185370da8 CFRunLoopRunSpecific + 552
10 GraphicsServices               0x187353020 GSEventRunModal + 100
11 UIKit                          0x18f35178c UIApplicationMain + 236
12 _                              0x100931208 main (DepositCoordinator.swift:8)
13 libdyld.dylib                  0x184e01fc0 start + 4

将file1转换为sed脚本并针对file2运行。

脚本匹配file2中的键,并用file1中匹配行的内容替换该行。