-1

I have the following output

$ cat /proc/net/route
Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT                               
br-lan  03043836        C0A80101        0007    0       0       5       FFFFFFFF        0       0       0                                  
br-lan  C0A80100        00000000        0001    0       0       0       FFFFFF00        0       0       0  

I use the awk to extract the line containing the Destination 03043836 and the Mask FFFFFFFF and then I use the awk another time to display the first elment from the extracted line:

$ dest=03043836; mask=FFFFFFFF; va=1;
$ cat /proc/net/route | awk '$2=="'"$dest"'" && $8=="'"$mask"'"' | awk '{print $'"$va"'}'
br-lan

Now I want to gather both awk commands in only one awk command. How to do that?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    You're joking, right? You asked a question yesterday (http://stackoverflow.com/questions/23370085/regexp-error-when-filtering-command-output), got the right answer and then took the wrong answer from the associated comments (where everyone agreed that is NOT the right approach) and decided to implement THAT instead of the right one??? See @Ashkan's answer. – Ed Morton Apr 30 '14 at 12:12

3 Answers3

4
dest=03043836; mask=FFFFFFFF; va=1;
awk -v dest="$dest" -v mask="$mask" -v va="$va" '$2==dest && $8==mask {print $va}' /proc/net/route

-v is used to assign a value to a variable.

a5hk
  • 7,532
  • 3
  • 26
  • 40
2

You can combine all three of them (including the cat which is needless here):

dest=03043836; mask=FFFFFFFF; va=1;

awk -v dest="${dest}" -v mask="${mask}" -v va="${va}" {print $va}' /proc/net/route

P.P
  • 117,907
  • 20
  • 175
  • 238
  • @EdMorton Ashkan posted only a few seconds after me. I just didn't want to be seen to be copying it. Really, passing variable *the* way. I'll take your downvote as a compliment ;-) – P.P Apr 30 '14 at 12:22
  • @EdMorton I updated on your insistence. While you are there, you should also use `{}` (`${var}`)to expand variables instead of `$var` to avoid surprises. – P.P Apr 30 '14 at 12:52
  • @EdMorton On that logic, passing variables to awk doesn't add any value either as OP mentioned exactly what he is passing for each of the variables, which are safe as-is. – P.P Apr 30 '14 at 13:05
  • @EdMorton How? For e.g., `val=1; somefunc $val1` (correct one being `somefunc "${val}1"`) it'll simply pass 0 if `val1` is not defined and produce wrong results depending on what's expected. I won't say this failed *immediately* or *clearly*. – P.P Apr 30 '14 at 13:16
  • In the example, `somefunc` expected 11 while passed one is empty or some value(depending on `val1`). Not sure how you still see this clearly. The program will produce incorrect results without any obvious hint. What I find contradictory is that you say "does add value" as OP doesn't use such variables yet you carry on to say *when* the variables contain strange values awk will blow *despite* OP has clearly mentioned values for variables. The fact is I don't disagree with using `-v` or particular usage of curly braces. Just your contradiction when you wrote examples based on variables blowing. – P.P Apr 30 '14 at 13:29
  • If you have some minor coding convention you like to use in your shell scripts that you find protects you from errors you find difficult to debug, go for it, but what we're discussing for passing shell variable values to awk is as important as always quoting shell variables in as much as it's simple to do it the right way, it protects you from bizarre errors given some input values, the ways in which it can fail are non-obvious, and if you learn to do it the wrong way you almost certainly will regret it one day. – Ed Morton Apr 30 '14 at 13:44
1

This is not an answer, just a couple of small examples demonstrating why you should use Ashkan's answer:

---------
$ x="hello world"

$ awk -v y="$x" 'BEGIN{print y}'
hello world

$ awk 'BEGIN{print "'"$x"'"}'
hello world

---------
$ x="hello
world"

$ awk -v y="$x" 'BEGIN{print y}'
hello
world

$ awk 'BEGIN{print "'"$x"'"}'
awk: cmd. line:1: BEGIN{print "hello
awk: cmd. line:1:             ^ unterminated string
awk: cmd. line:1: BEGIN{print "hello
awk: cmd. line:1:             ^ syntax error

---------
$ x="hello world\\"

$ awk -v y="$x" 'BEGIN{print y}'
hello world\

$ awk 'BEGIN{print "'"$x"'"}'
awk: cmd. line:1: BEGIN{print "hello world\"}
awk: cmd. line:1:             ^ unterminated string
awk: cmd. line:1: BEGIN{print "hello world\"}
awk: cmd. line:1:             ^ syntax error

How would you prefer your script to behave in the latter 2 cases?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185