bash 如何使用shell脚本在文本文件中添加空行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18527072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add empty line in text file using shell script?
提问by user2653831
this is my code, test.txt file contains more empty lines and empty space apart from tab , i want to remove that empty space and empty lines as well , but i need one empty lines before starting st^
, how to do it?
这是我的代码,test.txt 文件除了 tab 之外还包含更多的空行和空格,我也想删除那个空格和空行,但是在开始之前我需要一个空行st^
,该怎么做?
sed "s/^[ ]*//" -i test.txt
cat > /tmp/tt.txt
sed '/^$/d' test.txt > /tmp/tt.txt
echo " " >> test.txt
echo " " >> /tmp/tt.txt
mv /tmp/tt.txt test.txt
iam getting output like
我得到的输出像
st^flower
p^rose
p^jasmine
st^animals
p^bear
p^elephant
i want output like
我想要输出像
st^flower
p^rose
p^jasmine
st^animals
p^bear
p^elephant
采纳答案by akozin
i've read your comment "it's giving each and every line space,but i want only before st^". So you can insert new line before st^ with this code:
我已经阅读了您的评论“它给了每一行空间,但我只想要在 st^ 之前”。因此,您可以使用以下代码在 st^ 之前插入新行:
$cat /tmp/tt.txt | sed 's/^st\^/\n$ echo '
st^flower
p^rose
p^jasmine
st^animals
p^bear
p^elephant' | sed 's/^st\^/\necho -en '\n'
/g'
/g'
For example:
例如:
echo -e "\n"
Works fine for me.
对我来说很好用。
回答by Grzegorz ?ur
To output empty line use this:
要输出空行,请使用以下命令:
##代码##e - interprets \n
电子口译 \n
n - outputs no empty line on the end, so there is no mess with double empty lines
n - 最后不输出空行,所以不会出现双空行
Use single quotes to prevent shell from interpreting chars as end of line.
使用单引号防止 shell 将字符解释为行尾。
回答by sr01853
With double quotes, it must work
使用双引号,它必须工作