0

I have the following TSV and newlines string assigned to a variable in bash:

TAGS    Product3    qwerty  text    Desc3
TAGS    Product1    qwerty  text    Desc1
TAGS    Product2    qwerty  text    Desc2

I would like to extract the last column to a new string, and it has to be product ordered by my product input, for example:

Product1,Product2,Product3 will have to output: Desc1,Desc2,Desc3

What would be the best approach to accomplish this?

2
  • Expected output would be just this: Desc1,Desc2,Desc3 Commented Mar 30, 2017 at 22:46
  • Put expected output in the question and delete comment. Commented Mar 31, 2017 at 1:21

4 Answers 4

1

echo "$tsv_data" | awk '{print $2 " " $5}' | sort | awk '{print $2}' | paste -sd ',' -

This does the following steps in order:

  • Print the second and 5th argument (Product and Description) with a space between them.
  • Sort the input with sort (use gnu-sort if it can contain numbers)
  • Print only the description (in each line)
  • Join the lines together with paste

which will produce the following output:

Desc1,Desc2,Desc3
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a function that I suppose should do it:

get_descriptions() {
    local tsvstring="$1"
    local prodnames="$2"
    local result=()
    # read tsv line by line, splitting into variables
    while IFS=$'\t' read -r tags prodname val1 val2 desc || [[ -n ${prodname} && -n ${desc} ]]; do
        # check if the line matches the query, and if, append to array
        if grep -iq "${prodname}" <<< "${prodnames}"; then
            result+=("${desc}")
        fi
    done <<< "${tsvstring}"
    # echo the result-array with field separator set to comma
    echo $(IFS=,; echo "${result[*]}")
}

Then you can just use it like:

get_descriptions "${tsv_string_var}" "product1,product2"

Comments

0
echo "$var" | sort -k2 tags | cut -f5 | paste -sd,

Comments

0

sort + awk + paste pipeline:

echo "$tsv" | sort -nk2 | awk '{print $5}' | paste -sd',' -

The output:

Desc1,Desc2,Desc3

sort -nk2 - sorts the input by the second column numerically

awk '{print $5}' - prints out each fifth column

paste -sd',' - merge lines with ,

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.