Here's [a link] Using awk to find a string in a file
[2 - link] awk command to accept two variables as parameters and return a value
Here is the table:
----------------------------------------------------
Id Name CreationDate Comment
----------------------------------------------------
1 testing: 19.10.11 created by jag
2 develop 19.10.12 created by jag
3 array 19.10.12 created by jaguuuu
4 start123 19.10.12 created by akj
So I referred both of these, what I am trying to do is get the id number from first column and name from second column.
For example, If I take id=1 and name = testing ; if i find in 1st column id =1 and in 2nd column name = testing, then my output shall be testing.
This is the code, I wrote: a is a variable which has some output stored in it.
b=$(echo "$a" | awk -v var1=1 -v var2=testing '$1 ~ var1 && $2 ~ var2 {print $2}')
echo "$b"
But it not printing out anything. I thought it shall print out testing.
update: field1::::
1 testing: 19.10.11 created by jag
In our case, Input:
1 testing:
output:
testing:
if var1=1 and var 2 = testing both are found in same row just under column1("1") and column2 ("testing:")
Sorry for asking lot of questions - new to it, and trying to learn via writing code.
Note: table is stored in a variable a.
So, this should work:
x=$(echo"$a"|awk -v var1=1 -v var2=testing '$1 ~ var1 && $2 ~ var2 {print $2}')
echo"$x"
But wonder why it is not working. Will check few more stuff like you guys mentioned and get back to you guys tomorrow.
Thanks
a="1 testing:", the sample code you provided works.