Awk output to shell variable with shell variable as search pattern - bash

I'm trying to find (with awk) the IP of a specific ethernet interface using the hostname as a search patter (suffixed by the name of the ethernet interface). I wrote this little script but it outputs nothing and I don't understand why...
#!/bin/bash
name=$(hostname -s)-eth3
IP1=`awk -v var=$name '/var/ {print $1}' /etc/hosts`
echo $IP1

Could you please make few changes as shown following, which may help you.
#!/bin/bash
name=$(hostname -s)-eth3
IP1=$(awk -v var=$name '$0 ~ var{print $1}' "/etc/hosts")
echo "$IP1"
changes like backtick is not encouraged for storing values in bash variables $ should be used and use echo "$var" too.

Inside slashes, awk treats var as a literal string, not a variable.
Thus, replace:
/var/
With:
$0 ~ var
Thus use:
#!/bin/bash
name=$(hostname -s)-eth3
ip1=`awk -v var=$name '$0 ~ var {print $1}' /etc/hosts`
echo "$ip1"
Example
The first awk script below produces no match but the second does:
$ echo host-eth1 | awk -v var='host-eth1' '/var/ {print $1}'
$ echo host-eth1 | awk -v var='host-eth1' '$0 ~ var {print $1}'
host-eth1

Related

How to combine two awk commands into a single line

how can I combine the following two awk commands into a single line:
awk -F= '$1=="ID" { print $2 ;}' /etc/*release && awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/*release | xargs
Im trying to get the linux distribution and os in a single line, in the format distribution+version.
For example: ubuntu20.04, rhel7.5
With bash:
source /etc/os-release; echo "$ID $VERSION_ID"
You can capture each part into a variable and then print them out once you have processed the file:
awk -F= '$1=="ID"{id=$2}$1=="VERSION_ID"{vid=$2}END{print id,vid}' /etc/*release
I assume you don't want the quotes (as in your example), in that case:
awk -F '["=]' '$1=="ID" {printf("%s,",$3)} $1=="VERSION_ID" {printf("%s\n",$3)}' < /etc/*release

Bash variable in AWK [duplicate]

This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
I'm trying to pass variable in loop with awk command to find values. I have a file:
input.txt
1234|something|ohmygod
2345|urabura|kangura
9999|1234|xxxsecrets
shell command
cat input.txt | awk -F'|' '$1 ~ /1234/'
or
awk -F'|' '$1 ~ /1234/' input.txt
get first line from file as desired. Problem occurs when I try to print this via bash. When I simply test echo like:
echo `cat input.txt | awk -F'|' '$1 ~ /1234/'`
or
echo `awk -F'|' '$1 ~ /1234/' input`
I got desired output, but unfortunately when I try to pass variable inside it
variable1="1234"
echo `awk -F'|' '$1 ~ /"$variable1"/' input`
or
variable1="1234"
echo `awk -v var="$variable1" -F'|' '$1 ~ /var/' input`
it gives one empty line. Please suggest how to pass variable inside regex awk filter.
PS It is not duplicate question to: How do I use shell variables in an awk script? due to fact that I have knowledge how to use variable in AWK as I posted up here (-v parameter) but the question is how to PASS variable in REGEX in AWK (place between two slashes - echo awk -F'|' '$1 ~ /"$variable"/' input)
What you asked for:
awk -v var="$variable1" -F'|' '$1 ~ var' input
What you actually need:
awk -v var="$variable1" -F'|' '$1 == var' input
See http://cfajohnson.com/shell/cus-faq-2.html#Q24

AWK: Passing two arguments and weird error

I have made an awk implementation of grep -c ^str file and I want to pass the file and str arguments from a shell script. I am using awk -v twice to pass the arguments but I get a awk: cannot open var1 (No such file or directory) error.
I just can't get around it, I've been trying for almost an hour.
My code:
read -p "Give me a file name " file
read -p "Give me a string " str
awk -v var1="$file" -v var2="$str" 'BEGIN{print var1; print var2}{/^var2/}' var1 |
awk '{if ($0 != "/s") {count++}} END {print count}'
It should be:
awk -v var1="$file" -v var2="$str" 'BEGIN{print var1; print var2}{/^var2/}' $file
awk vars can only be acceded inside awk code (delimited by single quotes in this case) not at shell level where var1 means nothing.
Note that var2 value will be just a literal string between slashes /^var2/, use $0 ~ "^"var instead to access var2 value.
In fact, your awk code can be rewritten as:
awk -v var="$str" '$0 ~ "^"var && $0 != "/s"{count++}END{print count}' $file

Bash: AWK - $1 as first parameter of shell script

I spent on this 2 hours and get nothing. I want to get $1 and $2 as a first command line input of shell script, but I couldn't manage this. And $3 and $0 would be columns in awk. I try different methods but nothing works for me.
awk -F':' -v "limit=1000" '{ if ( $3 >=limit ) gsub("~/$1/",~/$2/); print \$0}' file.txt
the cleanest method is to explicitly pass the values from shell to awk with awk's -v option:
awk -F: -v limit=1000 -v patt="~/$1/" -v repl="~/$2/" '
$3 >=limit {gsub(patt,repl); print}
' file.txt
When your awk line is part of a script file, and you want to use $1 and $2 from the script in your awk command, you should temporary stop the literal string with a single quote and start it again.
awk -F':' -v "limit=1000" '{ if ( $3 >=limit ) gsub("~/'$1'/",~/'$2'/); print $0}' file.txt
You didn't post any sample input or expected output so this is a guess but you probably want something like this:
awk -F':' -v limit=1000 -v arg1="$1" -v arg2="$2" '$3 >= limit{gsub("~/" arg1 "/","~/" arg2 "/"); print}' file.txt

Passing bash input variables to awk

Trying to pass a variable into awk from user input:
Have tried variations of awk -v with errors stating 'awk: invalid -v option', even though the option is listed in man files.
#! /bin/bash
read -p "Enter ClassID:" CLASS
read -p "Enter FacultyName:" FACULTY
awk '/FacultyName/ {print}' data-new.csv > $FACULTY.csv
awk -vclass=${CLASS} '/class/ {print}' data-new.csv >> $FACULTY.csv
echo Class is $CLASS
echo Faculty member is $FACULTY
Some versions of awk require a space between the -v and the variable assignment. Also, you should put the bash variable in double-quotes to prevent unwanted parsing by the shell (e.g. word splitting, wildcard expansion) before it's passed to awk. Finally, in awk /.../ is a constant regular expression (i.e. /class/ will search for the string "class", not the value of the variable "class"). With all of this corrected, here's the awk command that I think will do what you want:
awk -v class="${CLASS}" '$0 ~ class {print}' data-new.csv >> $FACULTY.csv
Now, is there any reason you're using this instead of:
grep "$CLASS" data-new.csv >> $FACULTY.csv
Your script is not clear to me, but these all work:
CLASS=ec123
echo | awk -vclass=$CLASS '{print class}'
echo | awk -vclass=${CLASS} '{print class}'

Resources