Bash 如何实时更改txt文件

Bash 如何实时更改txt文件,bash,if-statement,for-loop,awk,sed,Bash,If Statement,For Loop,Awk,Sed,我需要关于bashshell的帮助 我想使用info.txt实时更改snmptrap.txt中的内容,如result.txt content add snmptrap.txt实时添加到行尾,因此我希望在result.txt上添加更改内容 smtptrap.txt: 2015/10/07 "General" 10.2 - ZBXTRAP 10.1 hello1 AAA05056 2015/10/07 "General" 10.2 - ZBXTRAP 10.1 hello2 hello2 AA

我需要关于bashshell的帮助 我想使用info.txt实时更改snmptrap.txt中的内容,如result.txt

content add snmptrap.txt实时添加到行尾,因此我希望在result.txt上添加更改内容

smtptrap.txt:

2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello1 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello2 hello2 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello3 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello4 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello5 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test1 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test3 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test4 hello2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test5 hello2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello1 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello2 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello3 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello4 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello5 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test1 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test2 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test3 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test4 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test5 AAA05010 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello1 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello2 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello3 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello4 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 hello5 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test1 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test2 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test3 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test4 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 10.1 test5 AAA05000
我要更改匹配的use info.txt info.txt:

100.300;AAA05056
100.200;AAA05010
100.100;AAA05000
result.txt应如下所示:

2015/10/07  "General" 10.2 - ZBXTRAP 100.300 hello1 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 hello2 hello2 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 hello3 AAA05056 
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 hello4 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 hello5 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 test1 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 test2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 test3 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 test4 hello2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.300 test5 hello2 AAA05056
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 hello1 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 hello2 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 hello3 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 hello4 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 hello5 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 test1 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 test2 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 test3 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 test4 AAA05010
2015/10/07  "General" 10.2 - ZBXTRAP 100.200 test5 AAA05010 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 hello1 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 hello2 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 hello3 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 hello4 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 hello5 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 test1 AAA05000 
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 test2 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 test3 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 test4 AAA05000
2015/10/07  "General" 10.2 - ZBXTRAP 100.100 test5 AAA05000
根据你的例子

 $ awk -F"[; ]" 'NR==FNR{a[$2]=$1;next}  $9 in a{$7=a[$9]}1' file2 file1       
将完成此工作,但是您的输出与提供的映射不匹配,请检查

捕获输入文件更新的一种方法是使用
tail-f
。比如说

 $ awk ... file2 <(tail -f file1) >> output &
这可能适用于您(GNU-sed):

这会将第二个文件转换为用于转换第一个文件的sed脚本


第二个文件的第二个字段用作查找,第二个文件的第一个字段用作替换第一个文件中的第六个字段。

我想您已将AAA05000替换为AAA05056谢谢。和内容添加snmptrap.txt的行结束时,在实时,所以我想添加对result.txt的更改内容。你能帮个忙吗?实时意味着什么?每秒都在snmptrap.txt文件的新行末尾添加内容。明白了,一种方法是你可以
tail-f
将该文件作为后台进程添加到awk脚本中,当有更新时,它可以将其写入。如何使用tail-f awk-f“[;]”NR==FNR{a[$2]=1;next}$NF a{$10=a[$NF]}1'info.txt snmptraps.txt | tail-1我制作了这个,但不起作用
 $ tail -f output
sed -r 's#^([^;]*);(.*)#/\2/s/\\S+/\1/6#' file2 | sed -rf - file1