Append to same line in Bash

前端 未结 4 1983
一个人的身影
一个人的身影 2021-02-20 06:50

The file letters.csv contains:

b,a,c,

The file numbers.csv contains:

32
34
25
13

I would like to append numbe

4条回答
  •  渐次进展
    2021-02-20 07:39

    You can do it with paste alone.

    First, convert contents in numbers.csv to comma-separated values. -s is the serial option and -d, specifies comma as delimiter:

    $ paste -sd, numbers.csv
    32,34,25,13
    

    Then append this output to letters.csv by specifying an empty delimiter and process substitution:

    $ # use -d'\0' for non-GNU version of paste
    $ paste -d '' letters.csv <(paste -sd, numbers.csv) > tmp && mv tmp letters.csv
    $ cat letters.csv
    b,a,c,32,34,25,13
    


    To modify sed command posted in OP, use command substitution:

    $ sed -i -e "s/$/$(sed -e :a -e '{N; s/\n/,/g; ta}' numbers.csv)/" letters.csv
    $ cat letters.csv
    b,a,c,32,34,25,13
    

提交回复
热议问题