1

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

1
  • Considering a="1 testing:", the sample code you provided works. Commented Sep 10, 2014 at 23:15

1 Answer 1

2

Putting $ in front of variable names to get their value is shell syntax. awk is not shell. Just use the variable name, just like you would in C or most other languages.

awk -v var1=1 -v var2=testing '$1 ~ var1 && $2 ~ var2 {print $2}'

Chances are, though, this is not what you really want to do. Post another question with sample input and expected output and we can show you the right way to do it.

Using the sample input from your updated question:

$ cat file
----------------------------------------------------
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

$ awk -v var1=1 -v var2=testing '$1 ~ var1 && $2 ~ var2 {print $2}' file
testing:
Sign up to request clarification or add additional context in comments.

11 Comments

Correct this is not what I want.. It is still not printing anything.
Then all we can do is wait for you to post sample input and expected output to help us understand what you are really trying to do.
I updated my answer to show it running against your posted input file and it does produce the desired output. There's really only a couple of possibilities: 1) you are using a broken awk, or 2) your input file has control chars in it. For the former, tell us what OS and awk version you are using, for the latter, run cat -v file on your input file to see if any control chars show up.
this approach help.. I shut everything down, and starting everything from scratch. Had a "." in the code somewhere which was not even reaching here. thanks for being patient with me.
I highly recommend the books Effective Awk Programming, Third Edition, by Arnold Robbins and Shell Scripting Recipes by Chris Johnson to learn awk and shell respectively.
|

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.