0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.