LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2018, 08:43 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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

Any ideas?
 
Old 03-09-2018, 09:36 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 ).

  1. ip address show : shows all information
  2. grep 'pattern' : gets you to the specific area where you want to be within all of the information shown.
  3. 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.

Last edited by BW-userx; 03-09-2018 at 09:45 AM.
 
Old 03-09-2018, 09:57 AM   #3
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.
 
Old 03-09-2018, 10:13 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by vincix View Post
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.

Code:
whatIwant=$1
whatIamUsing=eth0

awk -v var="$whatIwant" ....
or
awk -v var="$whatIamUsing" ...
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.


if that is what you are trying to do.
Code:
$ ./positionalparameter wlan0
wlan0:
134.44.34.321/26

Last edited by BW-userx; 03-09-2018 at 10:52 AM.
 
Old 03-09-2018, 12:02 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You could select device in ip show command

Code:
d=eth0
ip a show dev $d | awk -F "[ /]*" '/inet/{print $3}'

Last edited by keefaz; 03-09-2018 at 12:05 PM.
 
1 members found this post helpful.
Old 03-09-2018, 12:54 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by vincix View Post
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.
 
Old 03-09-2018, 01:51 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by vincix View Post
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.
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.
 
1 members found this post helpful.
Old 03-09-2018, 07:30 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
While the smart solution is in post#5,
here is how you can handle it with an awk variable
Code:
ip address show | awk -v iface="$1" -F "[ /]*" '$0~("inet.*" iface) { print $3 }'
Code:
ip address show | awk -v iface="$1" -F "[ /]*" 'BEGIN {srch=("inet.*" iface) } $0~srch { print $3 }'
The ~ operator makes the string an RE.
 
Old 03-09-2018, 07:50 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)?
 
Old 03-09-2018, 08:22 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by grail View Post
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 View Post
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'.
 
Old 03-09-2018, 08:39 PM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by rknichols View Post
You might want to reread:
this part is the imporant part.
Quote:
Originally Posted by vincix
So the point is to use bash positional parameters.
everything else is just using the bash positional parameters.
$1
Code:
#!/bin/bash

#ip address show | awk -v doop="$1"  '$0~doop{print $2}'

#ip address show | awk -F "[ /]*" '/inet.*'$1'/ { print $3 }'

ip address show | awk -v iface="$1" -F "[ /]*" 'BEGIN {srch=("inet.*" iface) } $0~srch { print $3 }'

Last edited by BW-userx; 03-09-2018 at 08:41 PM.
 
Old 03-09-2018, 09:00 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by rknichols View Post
You might want to reread:
My bad

In that case I would do:
Code:
ip a s "$1" | awk '/inet/{print gensub(/\/.*/,"",1,$2)}'
 
Old 03-09-2018, 09:42 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
My bad

In that case I would do:
Code:
ip a s "$1" | awk '/inet/{print gensub(/\/.*/,"",1,$2)}'

it might work with eth0?
Code:
$ ./positionalparameter wlan0
1XX.3X1.X2.322
fe80::224:d7ff:fecf:9ea4
its using these two to get the output.
Code:
inet
inet6

Last edited by BW-userx; 03-09-2018 at 09:49 PM.
 
Old 03-09-2018, 10:47 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by BW-userx View Post
it might work with eth0?
Code:
$ ./positionalparameter wlan0
1XX.3X1.X2.322
fe80::224:d7ff:fecf:9ea4
its using these two to get the output.
Code:
inet
inet6
And op made no mention of not wanting ipv6 addresses ... or did I miss that too
If that is a requirement, simply put a space after the 't'
 
1 members found this post helpful.
Old 03-09-2018, 11:03 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by grail View Post
And op made no mention of not wanting ipv6 addresses ... or did I miss that too
If that is a requirement, simply put a space after the 't'
I think OP step out for a coffee and has not returned, yet.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Positional Parameter issue in Bash script torrid Programming 7 01-31-2017 04:41 AM
how to ignore resolving $1 in awk as the positional parameter raj k yadav Programming 7 03-17-2015 08:54 PM
[SOLVED] C++ how to get a string parameter from another program with '|' or < in bash embeddedPROJECTS Linux - General 2 11-10-2013 04:44 PM
[SOLVED] Bash: extended regex pattern 'NOT' disabled inside parameter expansion? romagnolo Linux - General 4 02-22-2012 04:39 PM
trouble with positional parameter $0 on bash. linux distro: ubuntu breezy linrookie Linux From Scratch 2 05-07-2006 12:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration