在脚本中将SED -n与变量一起使用 [英] Using SED -n with variables in a script
本文介绍了在脚本中将SED -n与变量一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在脚本中使用sed,但是它一直失败.这些命令本身似乎可以正常运行,所以我不确定自己做错了什么:
I am trying to use sed in a script but it keeps failing. The commands on their own seem to work so I am not sure what I am doing wrong:
这在OS X(bash)的终端中
This is in Terminal in OS X (bash)
注意:file1.txt包含多行文本
NOTE: file1.txt contains multiple lines of text
N=1
sed -n $Np < file1.txt > file2.txt
CONTENT=$(cat file2.txt)
echo $CONTENT
如果我将$ N更改为1,则效果很好
If I change $N to 1, it works perfectly
sed -n 1p <file1.txt >file2.txt
CONTENT=$(cat file2.txt)
echo $CONTENT
给我
content of file2.txt
因此,基本上,我正在尝试将文本从文件的第1行复制到文件的第2行的开头...如果第2行还没有以第1行的内容开头.
So basically, I am trying to copy the text from line 1 of a file to the start of line 2 of a file ... IF line 2 does not already start with the content of line 1.
推荐答案
shell不知道您要的是$N
而不是$Np
.
The shell doesn't know that you want $N
and not $Np
.
执行此操作:${N}p
这篇关于在脚本中将SED -n与变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文