[SOLVED] interpret bash positional parameter within awk regex string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
interpret bash positional parameter within awk regex string
I've got the following line:
Code:
ip address show | awk -F "[ /]*" '/inet.*eth0/ { print $3 }'
I'm trying to replace 'eth0' with the first bash positional parameter, i.e. $1. I know this can be a little bit tricky in awk, in that you need to declare the variable beforehand with -v. The problem in this case is that the positional parameter would be part of a regex, like this: /inet.*$1/ Of course, I couldn't simply write $1 as such because it seems something different for awk. And if I did arg=$1 then awk -v some_var=$arg, it wouldn't work, "some_var" would be interpreted literally within the regex
the 'replace' part of what you're saying is confusing, but the code says find and print.
You're trying to get the ip for your eth0, I got wlan0 not eth0, so this still applies to the same just change the what you're looking for with a little help from grep ( wlan0 to eth0 ).
ip address show : shows all information
grep 'pattern' : gets you to the specific area where you want to be within all of the information shown.
awk : gets the specific information you want
Code:
$ ip address show | grep wlan0 | awk 'FNR == 2 {print $2}'
1xx.xx.xx.1xx/24
FNR is which line (down) , print $x is which column of that line. In case you or others reading this didn't know.
I don't understand what is confusing exactly about replacing 'eth0' with whatever first positional parameter you're going to call the bash script with. Maybe that was confusing, the fact that I didn't mention that it's supposed to be part of a small bash script, so that I can run it simply like this: "bash_script eth0" - that would show me the ip of 'eth0'.
Your solution does the same thing the line I've written already does. Moreover, I'm using one tool (awk), instead of grep + awk, even if the awk syntax becomes somewhat more complicated. So the point is to use bash positional parameters. That's what I'm looking for.
I don't understand what is confusing exactly about replacing 'eth0' with whatever first positional parameter you're going to call the bash script with. Maybe that was confusing, the fact that I didn't mention that it's supposed to be part of a small bash script, so that I can run it simply like this: "bash_script eth0" - that would show me the ip of 'eth0'.
Your solution does the same thing the line I've written already does. Moreover, I'm using one tool (awk), instead of grep + awk, even if the awk syntax becomes somewhat more complicated. So the point is to use bash positional parameters. That's what I'm looking for.
ok, let me try this. You're writing a bash script that takes in values, seeings how it is a script it does not have to be a one liner using only awk. SO if you're taking in that what are you looking for off the command line then using that for awk to use to find what it is you're wanting to print out.
Code:
./scriptName wlan0
changes wlan0 to eth0 no matter what you type in on the frist postion.
puts first positional parameter into another variable name then use that name to do your dirty work.
see what this does for you
Code:
#!/bin/bash
ip address show | awk -v doop="$1" '$0~doop{print $2}'
You might have to play with it, so don't be shy.
giving this more thought:
"I'm trying to replace 'eth0' with the first bash positional parameter,"
I am wanting to use the first positional parameter in a bash script instead of hard coding the 'eth0' into my awk to find the device and print out the IP attached to it by piping in the output from the 'ip address show' command into awk.
I'm trying to replace 'eth0' with the first bash positional parameter, i.e. $1.
...
Any ideas?
As has been said...or at least hinted at, why? Don't hard code anything, just use the variable populated with $1.
You might want to add something like:
Code:
if [[ ! $1 ]]
then
echo "usage is namofscript <nic>"
exit
fi
to your script to remind users that a parameter is required.
ip address show | awk -F "[ /]*" '/inet.*eth0/ { print $3 }'
I'm trying to replace 'eth0' with the first bash positional parameter, i.e. $1.
Just beak up the quoted string such that "$1" is outside it so that the shell can do the substitution.
Code:
ip address show | awk -F "[ /]*" '/inet.*'$1'/ { print $3 }'
At first glance, it looks as though $1 is enclosed in single quotes. It is not. There is a closing quote, then $1 (unquoted), then an opening quote. When that whole string gets passed to awk it will have $1 replaced by the first positional parameter. As others have pointed out, you should probably ensure that said parameter is non-null in the script.
Not sure if anyone has tested any of these solutions, but on my machine they just return ALL ip's on the inet lines, ie not just whatever everyone thinks $1 is set to,
which by the way I would think is nothing (which also agrees with my findings)
The pipe is not a bash script nor does it create output for one.
@OP - Exactly what would $1 be in your mind as the output from your command (assuming it did work that way)?
Not sure if anyone has tested any of these solutions, but on my machine they just return ALL ip's on the inet lines, ie not just whatever everyone thinks $1 is set to,
which by the way I would think is nothing (which also agrees with my findings)
The pipe is not a bash script nor does it create output for one.
You might want to reread:
Quote:
Originally Posted by vincix
Maybe that was confusing, the fact that I didn't mention that it's supposed to be part of a small bash script, so that I can run it simply like this: "bash_script eth0" - that would show me the ip of 'eth0'.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.