2

I have to find all of the record which have a particular data which I am gonna pass as the command line argument.

awk expression is like this(Date is in this format :'02/08/2013')

cat records.txt| awk -F ',' '$4 ~ /02/08/2013/ {print $1 $2}'

Here 4th column is the date column.

What I want to do is that, provide the date as the first argument and compare it.

I tried this,But it is not working.

cat records.txt| awk -F ',' -v awkvar="$1" '$4 ~ /^"awkvar/ {print $1 $2}'

Here the date column starts with " quote, so I am telling to look for the records who start with "+awkvar the given date.

Can anyone help me with this?

Edit:

awk -F ',' -v var1="$1" '$4 ~ /^"2013/ {print $1 $2}' {This one is working, as I am directly comparing the record with 2013}

when I do this

awk -F ',' -v var1="$1" '$4 ~ /^"var1/ {print $1 $2}' , it does not return anything, what is the difference.

1 Answer 1

4

To pass variables to awk, use

awk -v awkvar=$value '{print awkvar}'

That said, no need to pipe cat | awk (useless use of cat) so finally :

awk -F, -v awkvar="$1" '$4 ~ "^\""awkvar {print $1 $2}' records.txt
Sign up to request clarification or add additional context in comments.

6 Comments

yes that is what I did, I updated the post as well, Do you think it should work? I have been trying this since yesterday?
when I do this cat data.csv| awk -F ',' -v var1="$1" '$4 ~ /^"2013/ {print $1 $2}' , I get the output BUT when I am doing this matching with first argument cat data.csv| awk -F ',' -v var1="$1" '$4 ~ /^"var1/ {print $1 $2}', It does not give me anything
Many thanks It worked, Could you please explain what happened there?what happened to the slashes and what is the issue with double quotes?
There's no real issue, just need to juggle with backslash and quotes. The slash is not mandatory and can be replaced by double quotes like in my snippet. It's more convenient for variable handling. For further informations : grymoire.com/Unix/Awk.html
@sputnick:awk -v not working on my Unix.I tried 1st command "awk -v awkvar=$value '{print awkvar}'"
|

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.