庆典:在一个文本文件替换整个行 [英] bash: replace an entire line in a text file
问题描述
我有我想要一个bash脚本来替换在文件中的整条生产线的情况。
行号始终是相同的,因此,可以是一个硬$ C $光盘变量
I have a situation where I want a bash script to replace an entire line in a file. The line number is always the same, so that can be a hard-coded variable.
我并不是想取代某些子串在该行,我只是想用一个新行完全替代该行。
I'm not trying to replace some sub-string in that line, I just want to replace that line entirely with a new line.
是否有这样做的(或简单的东西可以被扔进.SH脚本)的任何bash的方法。
Are there any bash methods for doing this (or something simple that can be thrown into a .sh script).
推荐答案
不是最大的,但是这应该工作:
Not the greatest, but this should work:
sed -i 'Ns/.*/replacement-line/' file.txt
其中, N
应该由你的目标行号代替。这代替了原来的文件的行。要保存在不同的文件中的文本改变,删除 -i
选项:
where N
should be replaced by your target line number. This replaces the line in the original file. To save the changed text in a different file, drop the -i
option:
sed 'Ns/.*/replacement-line/' file.txt > new_file.txt
这篇关于庆典:在一个文本文件替换整个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!