Search code examples
bashawkdelimiterpastecut

Using cut multiple times and concatenating the results using multiple delimiters?


If I have a file:

c1 c2 c3 c4 c5 c6
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18 

And I only want the first, second, fourth and fifth columns into one line but separated by different delimiters..

For example line 1 would be: 1;2:4-5 Line 2 would be: 7;8:10-11 Line 3 would be: 13;14:16-17

I think with the same delimiter the command would be something like:

paste --delimiter=':' <(cut -f1 file.txt) <(cut-f2 file.txt) <(cut -f4 file.txt) <(cut -f5 file.txt)

The result should be in an array such that each line is a separate entry in the array

IFS='\n'; echo "${array[*]}"

1;2:4-5
7;8:10-11
13;14:16-17

I'm thinking awk might be able to achieve this but I'm unable to come up with it...


Solution

  • To make awk print each line like you specified, use

    awk '{ print $1 ";" $2 ":" $4 "-" $5 }' filename
    

    Or, to exclude the header line,

    awk 'NR > 1 { print $1 ";" $2 ":" $4 "-" $5 }' filename
    

    To get the results of that into a bash array:

    while IFS='\n' read line; do array+=("$line"); done < <(awk '{ print $1 ";" $2 ":" $4 "-" $5 }' filename)