2

I have a TSV table and I'm trying to use bash to extract values from a specific row based on the user's input, and then assign those values to variables for use later.

Example table (columns are tab-delimited):

NAME     FOOD     TIME
Eric     pasta    8pm
Sally    bread    11am
Jeff     ribeye steak with fries    10pm

My goal:

echo Enter your name
read name
echo Hello, $name

(Select the row that matches the $name value and assign FOOD and TIME to $food and $time respectively)

echo You'll be having $food at $time

I did some reading and perhaps awk might be able to accomplish this, but I'm completely lost. How should I go about doing this?

3 Answers 3

3

Any command that can pull the 2x desired values out and display then on one line should work; from here you can read these 2x values into your variables with the read command.

Sample data:

$ cat mytsv                              # tab-delimited columns
NAME     FOOD     TIME
Eric     pasta    8pm
Sally    bread    11am
Jeff     ribeye steak with fries      10pm

We'll start by using awk to pull out the 2 fields of interest, making sure to use an output field separator that does not exist in the actual data (eg, OFS=":"):

$ name='Jeff'
$ awk -F'\t' -v ptn="${name}" 'BEGIN {OFS=":"} $1==ptn {print $2,$3}' mytsv
ribeye steak with fries:10pm

We can now combine IFS (set to :) and read to pull these values into our 2x variables, eg:

$ IFS=":" read -r food time < <(awk -F'\t' -v ptn="${name}" 'BEGIN {OFS=":"} $1==ptn {print $2,$3}' mytsv)
$ echo "${food}"
ribeye steak with fries
$ echo "${time}"
10pm
$ echo "You'll be having ${food} at ${time}"
You'll be having ribeye steak with fries at 10pm
Sign up to request clarification or add additional context in comments.

5 Comments

Tried using this one but I'm getting wrong outputs (some of my tsv values have spaces in them) and I can't seem to set the separator using awk -F. How do I fix this?
would need to see a more detailed set of input data that addresses what you're talking about; and if some of the values have white space then we'd need to see what constitutes a field separator (eg, head -4 <file> | od -c)
For instance a fourth row in the above table that says: Jeff ribeye steak with fries 10pm My field separator is always tab.
updated to work with the new sample data you've provided
I'd recommend a more obvious variable name, perhaps name, instead of ptn
3

Using GNU awk:

awk -F "\t" -v nam="$name" '$1==nam { printf "You will be having %s at %s\n",$2,$3 }' file

Set the field delimiter to tab and pass variable nam into awk using the variable name. When the first tab delimited field is equal to nam, print the 2nd and 3rd delimited fields in the format required

1 Comment

There's nothing GNU-specific there, it'll work in any POSIX awk.
2

A bash solution:

read -p 'Enter your name: ' yourName
echo "Hello, $yourName"

found=0
while IFS=$'\t' read -r name food time; do
  if [[ "$name" == "$yourName" ]]; then
    found=1
    break
  fi
done < input.tsv

if [[ $found -eq 1 ]]; then
  echo "You'll be having $food at $time"
else
  echo "Name $yourName not found"
fi

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.