我有我想要一个bash脚本来替换在一个文件中的整条生产线的情况。 行号始终是相同的,因此,其可以是硬编码的变量。
我并不想更换一些子串在这条线,我只是想用一个新行完全替代该行。
是否有这样做的(或简单的东西可以被扔进.SH脚本)任何bash的方法。
我有我想要一个bash脚本来替换在一个文件中的整条生产线的情况。 行号始终是相同的,因此,其可以是硬编码的变量。
我并不想更换一些子串在这条线,我只是想用一个新行完全替代该行。
是否有这样做的(或简单的东西可以被扔进.SH脚本)任何bash的方法。
不是最大的,但是这应该工作:
sed -i 'Ns/.*/replacement-line/' file.txt
其中N
应该由你的目标线号码代替。 这将替换原来的文件就行了。 要保存在不同的文件中的文本改变,删除-i
选项:
sed 'Ns/.*/replacement-line/' file.txt > new_file.txt
我实际使用这个脚本在我们公司的UNIX服务器上的cron文件替换一行代码一段时间回来。 我们执行它作为正常的shell脚本,也没有问题:
#Create temporary file with new line in place
cat /dir/file | sed -e "s/the_original_line/the_new_line/" > /dir/temp_file
#Copy the new file over the original file
mv /dir/temp_file /dir/file
这并不行号去的,但你可以轻松地在该行号之前切换到基于行号系统s/
和放置一个通配符代替the_original_line
。
让我们假设你想用文本“不同”,以取代线4。 您可以使用AWK像这样:
awk '{ if (NR == 4) print "different"; else print $0}' input_file.txt > output_file.txt
AWK认为输入是“记录”分为“田”。 默认情况下,一条线是一个记录。 NR
是可见的记录数。 $0
表示当前完整的记录(而$1
是从记录等的第一字段;默认字段是从行字)。
因此,如果当前的行数是4,打印字符串“不同”但除此之外,打印线不变。
在AWK,程序代码包含在{ }
在每个输入记录运行一次。
您需要在报价单引号的AWK程序,以保持外壳的试图解释的东西,如$0
。
编辑:从在下面的评论@chepner更短和更优雅的AWK程序:
awk 'NR==4 {$0="different"} { print }' input_file.txt
仅用于记录(即行)4号,以字符串“不同”更换整个记录。 那么对于每一个输入记录,打印记录。
显然,我的AWK技能生疏! 谢谢你,@chepner。
编辑:也看到@Dennis威廉姆森更短的版本:
awk 'NR==4 {$0="different"} 1' input_file.txt
这是如何工作的评论是这样解释的: 1
总是判断为真,所以相关的代码块始终运行。 但目前还没有相关的代码块,这意味着AWK做的只是打印整条生产线的默认操作。 AWK被设计为允许这样简洁的方案。
鉴于这种测试文件(test.txt的)
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Duis eu diam non tortor laoreet
bibendum vitae et tellus.
以下命令将取代第一行“文本换行”
$ sed '1 c\
> newline text' test.txt
结果:
newline text
consectetur adipiscing elit.
Duis eu diam non tortor laoreet
bibendum vitae et tellus.
更多信息可以在这里找到
http://www.thegeekstuff.com/2009/11/unix-sed-tutorial-append-insert-replace-and-count-file-lines/
在bash,取代N,M由行号和XXX YYY通过你想要什么
i=1
while read line;do
if((i==N));then
echo 'xxx'
elif((i==M));then
echo 'yyy'
else
echo "$line"
fi
((i++))
done < orig-file > new-file
编辑
事实上,在这个解决方案也存在一些问题,与字符“\ 0”“\ t”和“\”
“\ t”的,可以是通过将IFS =读之前解决:“\”,在与-r线的端
IFS= read -r line
但对于“\ 0”,该变量是被截断,没有纯的bash的溶液: 分配包含空字符串(\ 0)到在击可变但在普通的文本文件没有空字符\ 0
perl的将是一个更好的选择
perl -ne 'if($.==N){print"xxx\n"}elsif($.==M){print"yyy\n"}else{print}' < orig-file > new-file
从Chepner出色答卷。 它在bash shell中为我工作。
# To update/replace the new line string value with the exiting line of the file
MyFile=/tmp/ps_checkdb.flag
`sed -i "${index}s/.*/${newLine}/" $MyFile`
这里
index
-线路无
newLine
-我们要更换新的线串。
类似地下面的代码是用来读取在该文件中的特定线路。 这不会影响实际的文件。
LineString=`sed "$index!d" $MyFile`
这里
!d
-将删除比其他线的线没有$index
因此,我们将得到的输出,因为没有行字符串$index
的文件中。
# Replace the line of the given line number with the given replacement in the given file.
function replace-line-in-file() {
local file="$1"
local line_num="$2"
local replacement="$3"
# Escape backslash, forward slash and ampersand for use as a sed replacement.
replacement_escaped=$( echo "$replacement" | sed -e 's/[\/&]/\\&/g' )
sed -i "${line_num}s/.*/$replacement_escaped/" "$file"
}
在Mac上我用
sed -i '' -e 's/text-on-line-to-be-changed.*/text-to-replace-the=whole-line/' file-name
你甚至可以将参数传递给sed的命令:
test.sh
#!/bin/bash
echo "-> start"
for i in $(seq 5); do
# passing parameters to sed
j=$(($i+3))
sed -i "${j}s/.*/replaced by '$i'!/" output.dat
done
echo "-> finished"
exit
orignial output.dat:
a
b
c
d
e
f
g
h
i
j
执行./test.sh给出了新的output.dat
a
b
c
replaced by '1'!
replaced by '2'!
replaced by '3'!
replaced by '4'!
replaced by '5'!
i
j