简体   繁体   English

用于替换换行符的sed命令在solaris中不起作用,但在linux中工作

[英]sed command to replace newline character is not working in solaris but working in linux

I have following sed command working in linux box. 我有以下sed命令在linux框中工作。 but the same is not working in solaris box. 但同样不适用于solaris盒子。 Please correct me what the problem is ? 请纠正我的问题是什么?

a=`sed ':a;N;$!ba;s/\n/ /g' file.txt`

Throws, 抛出,

-bash-3.00$ a=`sed ':a;N;$!ba;s/\n/ /g' file.txt`
Label too long: :a;N;$!ba;s/\n/ /g

With sed on solaris you'll have to break it up like this: 使用solaris sed ,你必须像这样分解它:

sed -e ':a' -e 'N;$!ba' -e 's/\n/ /g' file.txt

According to man page: 根据man页:

b label  Branch to the : command bearing the label.
             If label is empty, branch to the end of
             the script.  Labels are recognized unique
             up to eight characters.

Since they recognize up to eight characters, if your label is shorter than you'll need to split your sed in multiple expressions. 由于它们最多可以识别八个字符,因此如果您的标签比您需要在多个表达式中拆分sed所需的标签短。

In the original sed , I think a label had to be on a line by itself. 在最初的sed ,我认为标签本身必须在一条线上。 From my very ancient sed & awk Nutshell book, it states: 从我非常古老的sed & awk Nutshell书中,它指出:

A lable is any sequence of up to seven character. 标签是最多七个字符的任何序列。 A label is put on a line by itself and beginning with a colon: 标签单独放在一行并以冒号开头:

:label

That means, you need to separate it from the rest of the script with multiple -e arguments or see if you have nawk or gawk on your Solaris box. 这意味着,您需要使用多个-e参数将其与脚本的其余部分分开,或者查看Solaris框上是否有nawkgawk Alternatively, since it appears you just want to replace all newlines with spaces, there are better tools for the job, such as tr , the translate utility, which should at least be as ubiquitous as sed : 或者,因为看起来你只想用空格替换所有换行符,所以有更好的工具,比如tr ,翻译实用程序,它至少应该像sed一样无处不在:

a=`tr '\n' ' ' <file.txt`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM