我有两个文件,file1.txt
和file2.txt
。我想使用sed
并在bash脚本中读取行。我想将file1.txt
中的第1行用户替换为file2.txt
中的第1行,依此类推......
FILE1.TXT:
xxxx1 <br>
xxxx2 <br>
xxxx3
FILE2.TXT:
{"Something":null,"ID":"user","group":{"id":"group1") <br>
{"Something":null,"ID":"user","group":{"id":"group1") <br>
{"Something":null,"ID":"user","group":{"id":"group1")
新的file2.txt应该是这样的:
{"Something":null,"ID":"xxxx1","group":{"id":"group1") <br>
{"Something":null,"ID":"xxxx2","group":{"id":"group1") <br>
{"Something":null,"ID":"xxxx3","group":{"id":"group1")
有什么好主意吗?
答案 0 :(得分:2)
使用awk
:
awk 'NR == FNR { user[FNR] = $1; next } { sub("user", user[FNR]); print }' file1.txt file2.txt
但是,您也可以使用paste
和sed
:
paste file1.txt file2.txt | sed -r 's/([^ ]+)([^{]+)(\{.*)user(.*)/\3\1\4/'
答案 1 :(得分:0)
尝试使用以下命令:
for data in `awk '{print NR":"$1}' file1.txt` ; do row=`cut -d":" -f1 <<< ${data}` ;user=`cut -d":" -f2 <<< ${data}` ;sed -i ''$row's/"user",/"'$user'",/g' file2.txt ; done