I have an awk command. I need to use i variable but my command does not work when I do.

"fechaName": "1","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "2","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",    "firstName": "beto",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "111","xxxxx": "John",
"fechaName": "4","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "555", "xxxxx": "John","xxxxx": "John",
"fechaName": "5","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",
"fechaName": "4","firstName": "gdrgo",   "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John", "xxxxx": "John",   "lastName": "222",dfg
"fechaName": "7","xxxxx": "John",   "xxxxx": "John",    "firstName": "beto2", "xxxxx": "John","lastName": "444", "xxxxx": "John","xxxxx": "John",

When I use 5 instead of "i" it works

awk  -v OFS='"' -v   FS='Name": "'     '{ for( i=2; i<=7; i++ ) if( match($2, /5"/) )  print $0 }'   sumacomando

this is my command

awk  -v OFS='"' -v   FS='Name": "'     '{ for( i=2; i<=7; i++ ) if( match($2, /**i**"/) )  print $0 }'   sumacomando

awk  -v OFS='"' -v   FS='Name": "'     '{ for( i=2; i<=7; i++ ) if( match($2, /i"/) )  print $0 }'   sumacomando
  • What is the expected output? – George Vasiliou Mar 13 '17 at 15:05
  • only need sort by fechaName 2 to 7 with my command not external methods like sort or asort – Alejandro Diego Mar 13 '17 at 15:13

You can not sort all the records just with one pass.

To answer your main question literally to match a field with a variable you just need to do match($2,i) without /.../ (or match($2, i "\"") for i followed by a ". Using normal string concatenation syntax).

Even this one will bring you correct results for a record containing "4":

awk '{i=4}$0 ~ "\"" i "\""' file1 #(string concatenation yelds to "4")

But this one , though it will work without error, will not automatically sort the data with one pass as you might expect:

awk '{for (i=1;i<8;i++) if ($0 ~ "\"" i "\"") print}' file1

To sort all the records you need first to load all records in memory and then print them in a sorting order.

This is a different approach that works with your sample file:

awk  -F"[\"]" '{a[$4]?a[$4]=a[$4] ORS $0:a[$4]=$0;last=NR}END{for (i=1;i<=last;i++) if (a[i]) print a[i]}' file1

See online test here

Using delimiter " text of first line "fechaName": "1",.... will split in 4 pieces and 1 (field $4) can be used directly as an integer.

The part a[$4]?a[$4]=a[$4] ORS $0:a[$4]=$0 is just an if-then-else:

if a[$4] (==> a[1] for first line) has already a value then add this line to the previous a[$4] contents using the Output Record Separator (ORS) which is a new line (or could be a litteral "\n") - else just apply $0 to a[$4]

Finally in end section we just print this array a.

We need to say here that sort will be hell faster than awk for this operation:

LC_ALL=C sort -t"\"" -k3 file1
  • @AlejandroDiego Welcome. I simplified a bit the solution. We don't need many delimiters. Just " is enough. – George Vasiliou Mar 13 '17 at 17:16

It seems you want to print the lines containing 1 to 7. That would be something like

awk '/"fechaName": "[1-7]"/ { print }'
  • thanks ; I understand but I need to modify this posted script for sorting purpose – Alejandro Diego Mar 13 '17 at 13:45
  • this command not sort print in same order – Alejandro Diego Mar 13 '17 at 13:46
  • @AlejandroDiego Then please write a suitable problem description. – Michael Vehrs Mar 13 '17 at 14:00
  • I know but I put in my description that I need pass variable inside match – Alejandro Diego Mar 13 '17 at 14:02
  • if( match($2, /i"/) – Alejandro Diego Mar 13 '17 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.