Issue
I am with a problem with the Shell script to compare the value of a variable with thousands of values in a file. For example, supposing I have a variable
vel=8.14205
and I have a file with four columns but the most important its the two first columns,
7.9 white 7.90001 white L
7.90001 white 7.90002 white L
7.90002 white 7.90003 white L
7.90003 white 7.90004 white L
7.90004 white 7.90005 white L
7.90005 white 7.90006 white L
7.90006 white 7.90007 white L
7.90007 white 7.90008 white L
7.90008 white 7.90009 white L
7.90009 white 7.9001 white L
7.9001 white 7.90011 white L
7.90011 white 7.90012 white L
7.90012 white 7.90013 white L
7.90013 white 7.90014 white L
.
.
.
8.05034 gray81 8.05035 gray81 L
8.05035 gray81 8.05036 gray81 L
8.05036 gray81 8.05037 gray81 L
8.05037 gray81 8.05038 gray81 L
8.05038 gray81 8.05039 gray81 L
8.05039 gray81 8.0504 gray81 L
8.0504 gray81 8.05041 gray81 L
8.05041 gray81 8.05042 gray81 L
8.05042 gray81 8.05043 gray81 L
8.05043 gray81 8.05044 gray81 L
8.05044 gray81 8.05045 gray81 L
.
.
.
8.69995 black 8.69996 black L
8.69996 black 8.69997 black L
8.69997 black 8.69998 black L
8.69998 black 8.69999 black L
8.69999 black 8.7 black B
if in some of the thousands of lines there is a line with
8.14205 gray84
then, I need to save it in another variable the "gray84".
Someone knows how can I do that?
I will be very grateful!
Solution
If you use the sample data that you provided in your question and add the line 8.14205 gray84
, one awk
option might be:
$ vel=8.14205
$ vel_color=$(awk -v vel="$vel" '{if ($1==vel) {print $2}}' src.dat)
$ echo "$vel_color"
gray84
Use the value stored in the vel
variable to compare to the value in the first field. When there is a match print the second field.
Answered By - j_b Answer Checked By - Willingham (WPSolving Volunteer)