awk ignore case isn't working - awk

I am using this code to get ip entries from host file with ignore case and it doesn't seem to work on AIX
Input file
172.23.1.230 enboprtpapzp04.digjam.com enboprtpapzp04
#172.23.0.33 enboprtpapzp04.digjam.com enboprt enboprtpapzp04
172.23.1.230 enboprtpapzp04.fixture.com enboprtpap enboprtpapzp04
awk -v client="$client" 'BEGIN {IGNORECASE = 1}{k=0; for (i=1;i<=NF;i++){if ($i==client){print $1}; k++}}' file
See the output below
client=ENBOPRTPAPZP04
awk -v client="$client" 'BEGIN {IGNORECASE = 1}{k=0; for (i=1;i<=NF;i++){if ($i==client){print $1}; k++}}' file
Nothing comes up
expected output
grep -i ENBOPRTPAPZP04 /etc/hosts | awk '{print $1}' | grep -v "^#"
172.23.1.230
172.23.1.230

It works here:
$ awk -v client="$client" 'BEGIN{IGNORECASE = 1} $2==client && /^[^#]/{print $1}' your_hosts
172.23.1.230
172.23.1.230
Are you sure you are using GNU awk? If not, you could:
$ awk -v client="$client" 'tolower($2)==tolower(client) && /^[^#]/{print $1}' your_hosts
In the light of the resent - whoops, I meant recent - edits to the question and the mentioning of the loop in the comments I'll add this:
$ awk -v client="$client" '{for(i=1;i<=NF;i++) if(tolower($i)==tolower(client) && $1!~/^#/)print $1}' your_new_hosts
172.23.1.230
172.23.1.230
Also, check #EdMorton's last comment below for a non-looping version.
The check for the /^#/ could be outside of the action block in the condition part:
$ awk ... '!/^#/ {for(i=1;i<=NF;i++) if(tolower($i)==tolower(client)) print $1}' your_new_hosts

Related

AWKing or GREPing brackets [ ]?

I've searched all over and couldn't find a solution.
How would I awk or grep the following:
$ mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration
[/dev/cdc-wdm0] IPv4 configuration available: 'address, gateway, dns'
IP [0]: '11.22.333.44/55'
Gateway: '14.13.198.4'
DNS [0]: '172.17.1.101'
DNS [1]: '172.17.1.102'
DNS [2]: '172.17.1.101'
DNS [3]: '172.17.1.102'
So that I end up with:
11.22.33.44/55
I've tried a bunch of different combinations with both grep and awk and couldn't find a solution.
Using cat file as I don't have mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration:
$ cat file | awk -F"'" '/IP \[/{print $2}'
11.22.333.44/55
$ cat file | awk -F"'" '/Gateway/{print $2}'
14.13.198.4
or maybe this is all you need if the output of that command always looks like the example you posted:
$ cat file | awk -v RS= -F"'" '{print $5}'
11.22.333.44/55
$ cat file | awk -v RS= -F"'" '{print $8}'
14.13.198.4
You can do this in a single awk:
mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration |
awk '$1 == "IP" {gsub(/\047/, "", $NF); print $NF}'
11.22.333.44/55
something like this:
grep '[0-9]' file_with_text | awk '{print $NF}
grep [0-9] only lines with numbers and pipe the output into awk.
The $NF will return the last element.
If you want only the line that has the /, just add it to grep [0-9]/.
Also, for a complete answer, you can pipe the output of the command into grep:
mbimcli -d /dev/cdc-wdm0 -p --query-ip-configuration | grep '[0-9]/' | awk '{print $NF}
I would harness tr for preprocessing and GNU AWK for processing, let file.txt content be
IP [0]: '11.22.333.44/55'
Gateway: '14.13.198.4'
DNS [0]: '172.17.1.101'
DNS [1]: '172.17.1.102'
DNS [2]: '172.17.1.101'
DNS [3]: '172.17.1.102'
then
cat file.txt | tr -d "'" | awk '/IP/{print $NF}'
output
11.22.333.44/55
Explanation: use tr to delete ' then awk to print last column ($NF) if row contain IP.
(tested in tr (GNU coreutils) 8.30 and GNU Awk 5.0.1)

Adding hashtag to files

I have a awk program add_hashtag.awk
BEGIN{printf("#")}1
and a bash program
for file in *.asc; do awk -f add_hashtag.awk "$file" > "$file"_in; done
that add hashtag into file. It works, however, I would like to get files with same names. When I run
for file in *.asc; do awk -f add_hashtag.awk "$file" > "$file"; done
I get files only with #.
How to do that? Thank you
Could you please try following.
for file in *.asc; do awk -f add_hashtag.awk "$file" > "temp_file" && mv "temp_file" "$file"; done
I am going with approach where creating a temp_file for output and later renaming it to Input_file so that there will not be any danger of losing or truncating actual Input_file. Also it will not rename temp_file to actual Input_file until/unless awk command is a success(with use of &&)
With gawk 4.1.0 version or so try(haven't tested it since no samples were given):
awk -i inplace -f add_hashtag.awk *.asc
OR in case you want to inplace edit files along with taking their backup:
awk -i inplace -v INPLACE_SUFFIX=.backup -f add_hashtag.awk *.asc

awk for multiple pattern search & remove the first line from the output at the same time

I need to remove the first line of my output to be removed with awk. Below is the command and output has been displayed..
bash-3.2$ ldaplist -l hosts mylv104 | awk -F: '/cdsLocationDetail|.seemac.com|ipHostNumber/ {print $2}'
cn=mylv104+ipHostNumber=196.2.16.181,ou=hosts,ou=corp,ou=services,o=seemac.com
R3/C12/U21
mylv104.seemac.com
196.2.16.181
Though i got it with another awk with pipe but i don't want that..
bash-3.2$ ldaplist -l hosts mylv104 | awk -F: '/cdsLocationDetail|.seemac.com|ipHostNumber/ {print $2}' |awk 'NR>1'
R3/C12/U21
mylv104.seemac.com
196.2.16.181
You can set a flag when a line is matched, as shown below:
ldaplist -l hosts mylv104 | awk -F: '/cdsLocationDetail|.seemac.com|ipHostNumber/{if(!firstMatch){firstMatch=1;next;}print $2}'

Awk - field separator behavior

I have a file:
CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS
I want to print "$2"
Example 1
It works well:
$ awk -F '\\\\\\\\' '{print $2}' file
WINDOWS
Example 2
It works well:
$ awk -F '\\\\'+ '{print $2}' file
WINDOWS
Example 3
Does not print.
$ awk -F "\\\\\\\\" '{print $2}' file
.
Example 4
So it works well:
$ echo "CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS" | awk -F '\' '{print $2}'
WINDOWS
----------------------------------------------------------------------------------
$ echo "CreateSec,explorer.exe,\\WINDOWS\\system32\\verclsid.exe,SUCCESS" | awk -F "\\" '{print $2}'
WINDOWS
Questions:
Why 3 example does not work?
How to properly save 3 example?
What is the difference between?:
a) awk -F '\\\\\\\\'
b) awk -F "\\\\\\\\"
Thank you for the explanation.
Regarding (3) — four backslashes is the difference:
Inside single quotes, there are no metacharacters, so the shell passes 8 backslashes to awk.
Inside double quotes, the backslash escapes the following character, so the shell passes 4 backslashes to awk.

awk + OR operation in awk syntax

I created the following awk command in order to print only the line that match the host and the ETH parameters
my problem is that I don’t know which eth1-8 is the real argument
How to print the line from the file by awk if ETH could be eth0 or eth1 or eth2 ....etc until eth8
HOSTNAME=linux1
LAN=eth0|eth1|eth2|eth3|eth4|eth5|eth6|eth7|eth8
awk -v host=$HOSTNAME -v ETH=$LAN '$2 == host && $3 == ETH' file
more file
192.17.200.10 linux1 eth0
192.17.200.10 linux1 eth1
192.17.200.11 linux2 eth2
192.17.200.12 linux3 eth3
192.17.200.13 linux4 eth4
192.17.200.14 linux5 eth5
192.17.200.15 linux6 eth6
192.17.200.16 linux7 eth7
192.17.200.17 linux8 eth8
using awk:
hostname=linux1
lan=eth
awk -v host="$hostname" -v lan="$lan" '$2==host && ($3~lan)' yourFile
actually for your problem, grep works too:
grep -P 'linux1\s.*?eth\d' yourFile
if you want to use the variables in grep:
grep -P "${hostname}\s.*${lan}\d" yourFile
Use a regular expression to specify the possible matches, e.g.:
awk '$2 == "linux1" && $3 ~ /^eth[0-8]$/'
In terms of the shell variable, you'll want something like:
LAN='^eth[0-8]$'
awk -v host=$HOSTNAME -v ETH=$LAN '$2 == host && $3 ~ ETH' file