Skip to content
Advertisement

Use values in a column to separate strings in another column in bash

I am trying to separate a column of strings using the values from another column, maybe an example will be easier for you to understand.

The input is a table, with strings in column 2 separated with a comma ,.
The third column is the field number that should be outputted, with , as the delimited in the second column.

Ben mango,apple 1
Mary    apple,orange,grape  2
Sam apple,melon,*   3
Peter   melon   1

The output should look like this, where records that correspond to an asterisk should not be outputted (the Sam row is not outputted):

Ben mango
Mary    orange
Peter   melon

I am able to generate the desired output using a for loop, but I think it is quite cumbersome:

IFS=$'n'
for i in $(cat input.txt)
do
    F=`echo $i | cut -f3`
    paste <(echo $i | cut -f1) <(echo $i | cut -f2 | cut -d "," -f$F) | grep -v "*"
done

Is there any one-liner to do it maybe using sed or awk? Thanks in advance.

Advertisement

Answer

The key to doing it in awk is the split() function, which populates an array based on a regular expression that matches the delimiters to split a string on:

$ awk '{ split($2, fruits, /,/); if (fruits[$3] != "*") print $1, fruits[$3] }' input.txt
Ben mango
Mary orange
Peter melon
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement