linux sed 替换换行,sed如何替换换行符(\n)?_unix_开发99编程知识库
快速回答:sed ':a;N;$!ba;s/n/ /g' file:创建标签'"'N将下一行附加到模式空间如果不是最后一行,则$分支(转到)标签"a"',!正规表达式替代/n/为新行,/ /空格,/g全局匹配,替代选项:使用bash,慢while read line; do printf"%s""$line"; done < file使用perl,和sed 速度一样perl -p -e 's
快速回答:sed ':a;N;$!ba;s/n/ /g' file:创建标签'"'
N将下一行附加到模式空间
如果不是最后一行,则$分支(转到)标签"a"',!
正规表达式替代/n/为新行,/ /空格,/g全局匹配,
替代选项:
使用bash,慢while read line; do printf"%s""$line"; done < file
使用perl,和sed 速度一样perl -p -e 's/n/ /' file
使用tr,比sed快,只能替换一个字符tr 'n' ' ' < file
使用paste ,和tr速度一样,只能替换一个字符,paste -s -d ' ' file
使用awk,和tr速度一样awk 1 ORS=' ' file
在此类情况下不添加换行符。 因此,脚本sed 's/n//' file # to delete newlines from each line
sed 's/n/foon/' file # to add a word to the end of each line
要执行上述任务,
请使用以下脚本之一:tr -d 'n' < file # use tr to delete newlines
sed ':a;N;$!ba;s/n//g' file # GNU sed to delete newlines
sed 's/$/ foo/' file # add"foo" to end of each line
因为GNU之外的其他版本限制了
模式缓冲区,在这里是首选的Unix'tr'实用程序,
自从GNU sed
版本3.02.80现在支持此语法:sed '/start/,+4d' # to delete"start" plus the next 4 lines,
更多推荐




所有评论(0)