我有2个文件:
File_1.txt:
John
Mary
Harry
Bill
Run Code Online (Sandbox Code Playgroud)
File_2.txt:
My name is ID, and I am on line NR of file 1.
Run Code Online (Sandbox Code Playgroud)
我想创建四个看起来像这样的文件:
Output_file_1.txt:
My name is John, and I am on line 1 of file 1.
Run Code Online (Sandbox Code Playgroud)
Output_file_2.txt:
My name is Mary, and I am on line 2 of file 1.
Run Code Online (Sandbox Code Playgroud)
Output_file_3.txt:
My name is Harry, and I am on line 3 of file 1.
Run Code Online (Sandbox Code Playgroud)
Output_file_4.txt:
My name is Bill, and I am on line 4 of file 1.
Run Code Online (Sandbox Code Playgroud)
通常我会使用以下sed命令来执行此操作:
for q in John Mary Harry Bill
do
sed 's/ID/'${q}'/g' File_2.txt > Output_file.txt
done
Run Code Online (Sandbox Code Playgroud)
但这只会替换名称的ID,而不包括File_1.txt的行号.不幸的是,我的bash技能没有比这更进一步......对于包含文件1和2的命令的任何提示或建议?我确实需要包含文件1,因为实际上文件比这个例子大得多,但我想如果我知道如何用这个希望更简单的例子来解决其余的代码......很多提前致谢!
怎么样:
n=1
while read q
do
sed -e 's/ID/'${q}'/g' -e "s/NR/$n/" File_2.txt > Output_file_${n}.txt
((n++))
done < File_1.txt
Run Code Online (Sandbox Code Playgroud)
有关将输入重定向到代码块的信息,请参阅" 高级Bash脚本指南",也可以参阅有关双括号的部分以便进一步阅读.
| 归档时间: |
|
| 查看次数: |
2583 次 |
| 最近记录: |