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,dfgfechaName: 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,dfgfechaName: 7,xxxxx: John,   xxxxx: John,    firstName: beto2, xxxxx: John,lastName: 444, xxxxx: John,xxxxx: John,When I use 5 instead of i it worksawk  -v OFS='' -v   FS='Name: '     '{ for( i=2; i<=7; i++ ) if( match($2, /5/) )  print $0 }'   sumacomandothis is my command awk  -v OFS='' -v   FS='Name: '     '{ for( i=2; i<=7; i++ ) if( match($2, /**i**/) )  print $0 }'   sumacomandoawk  -v OFS='' -v   FS='Name: '     '{ for( i=2; i<=7; i++ ) if( match($2, /i/) )  print $0 }'   sumacomando
how I could pass variable inside awk match?
text processing;awk
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}' file1To 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]}' file1See online test hereUsing 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