Why doesn't this sed command do anything inside the bash script but work outside?

 # join pairs of lines side-by-side (like "paste")
 sed '$!N;s/\n/ /'

      

The above script comes from a large list of sed one-line files found in sourceforge.

I want to use it in a bash script, but it has no effect if used inside a script. If I connect the output of the script through it, it connects pairs of pairs next to each other as described.

Some characters need to be escaped, but I just can't "see" which character needs to be escaped to make it work inside a bash script.

Yoroshik Onegishimas!

Further..

#!/bin/bash
# numbers.sh

for X in 1 2 3 4 5 6 7 8 9 0
do
        echo $X
done

      

When using this script:

#!/bin/bash

./numbers.sh | sed '$!N;s/\n/ /'

      

works great.

1 2
3 4
5 6
7 8
9 0

      

Please allow me to regroup my thoughts on this.

Further...

I found a boolean error in the script that broke it.

0


a source to share


2 answers


It doesn't seem like an obvious problem to me without seeing your script. A quick test here and it worked fine inside a simple script:

#!/bin/bash
cat /etc/crontab | sed '$!N;s/\n/ /'

      

If you're trying to embed a command inside a string or variable, \ n would be a candidate for escape.



For what it's worth, there is rarely a "strong" case for creating bash-specific scripts on top of up-up / bin / sh posix compatible shell scripts, unless you really need extended containers (which is rare). You end up with a script that is significantly more portable across dozens of other posix / korn / bourne compatible shells (including bash).

Hooray! Sean

+3


a source


You are missing "$@"

the file name arguments - so is it only read from standard input?

What was the wrong behavior? Was the file just copied to standard output?



Works for me - under Cygwin. ' al

' is a program that lists its arguments, one per line.

$ al a b c d e f  | sed '$!N;s/\n/ /'
a b
c d
e f
$ cat xxx
sed '$!N;s/\n/ /'
$ al a b c d e f g | bash xxx
a b
c d
e f
g
$

      

+1


a source







All Articles