Ubuntu: Substitute cut command inside AWK script


I have a structured text file test.tr, and I would like to analyze it with an AWK script, but for a special reason I want to get all numbers surrounded with underscores, for example: _0_ should become 0. This is an example of input text file: s 0.000000000 _0_ RTR

So I'm trying to use the cut command inside the AWK script as follows:

# AWK script  BEGIN{   # initialization   highest_node_id = 0;  }  {      node_id = $3      node_id = node_id | cut -d "_" -f2 | cut -d "_" -f1 ;# this line causes error       if (node_id > highest_node_id) {          highest_node_id = node_id;      }  }  END{   #print results  }  

But I get an error

awk: `analyze.awk: line 37: syntax error at or near cut`  

whereas the same command works OK in a bash script.

What should I do?


cut works in a bash script, but shell is shell and awk is awk. You can't use perl commands in awk scripts either.

If you want external programs inside your script, you need to use the system command, but for what you seem to be doing here, you should use the string functions of awk like substr and gensub.


I finally got it to work by using substr:

tmp_node_id = $3;  node_id = substr(tmp_node_id, 2, (length(tmp_node_id) - 2));  printf ("node %s\n", node_id);  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »