简体   繁体   中英

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

I have following sed command working in linux box. but the same is not working in solaris box. 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:

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

According to man page:

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.

In the original sed , I think a label had to be on a line by itself. From my very ancient sed & awk Nutshell book, it states:

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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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