AjanO
AjanO

Reputation: 473

paste command in shell script

I'm trying to merge columns from two different files within the following script:

#!/bin/sh
#
#

echo "1 1 1" > tmp1
echo "2 2 2" >> tmp1
echo "3 3 3" >> tmp1

echo "a,b,c" > tmp2
echo "a,b,c" >> tmp2
echo "a,b,c" >> tmp2

paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)

The above script works fine when I run

bash test.sh

However, it does not work when I run

sh test.sh

and I get the following error message

test.sh: line 13: syntax error near unexpected token `('
test.sh: line 13: `paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)'

Could somebody explain what is the reason of this behaviour? Is there fix it? Thx.

Upvotes: 0

Views: 2217

Answers (3)

chepner
chepner

Reputation: 532428

<() provide a syntactic alternative to manually managing named pipes.

trap 'rm p1 p2' EXIT
mkfifo p1 p2
cut -d " " -f1 tmp1 > p1 &
cut -d " " -f 1-2 tmp2 > p2 &

paste -d':' p1 p2

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247210

You can implement this portably using file descriptors

while
    IFS=" " read -r x rest <&3
    IFS="," read -r y z rest <&4
do
    echo "$x:$y:$z"
done 3<tmp1 4<tmp2
1:a:b
2:a:b
3:a:b

Tested with dash

Upvotes: 0

heemayl
heemayl

Reputation: 42137

On your system, sh is presumably not set as bash (dash may be?).

The process substitution, <(), is bash-ism (comes from ksh actually), which is not defined by POSIX hence not portable.

So the shell you are using (sh) does not have the <() implementation, hence the syntax error on ( (as < indicates input redirection so the error actually being shown for the first ().

Upvotes: 1

Related Questions