Reputation: 57
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
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
Reputation: 512
echo "$tsv_data" | awk '{print $2 " " $5}' | sort | awk '{print $2}' | paste -sd ',' -
This does the following steps in order:
which will produce the following output:
Desc1,Desc2,Desc3
Upvotes: 1
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