C4t4
C4t4

Reputation: 57

Extract specific text out of bash variable containing TSV string

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?

Upvotes: 0

Views: 283

Answers (4)

RomanPerekhrest
RomanPerekhrest

Reputation: 92884

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 ,

Upvotes: 0

Michael Vehrs
Michael Vehrs

Reputation: 3363

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

Upvotes: 0

redxef
redxef

Reputation: 512

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

Upvotes: 1

Olli K
Olli K

Reputation: 1760

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"

Upvotes: 0

Related Questions