I have two files; can considered it as columns; but I need to exctract only the one that has ip address. If in both files there is text file; then can leave that intact. I tried "join" and awk without success.. Here is the output and the desired output.
FILE1 FILE2 Desired Output (FILE 3)
192.168.217.36 d0:d3:e0:cb:dc:f4 192.168.217.36
192.168.229.70 20:4c:03:59:ed:6a 192.168.229.70
No_IP_Found 192.168.197.181 192.168.197.181
192.168.230.81 192.168.230.81 192.168.230.81
No_IP_Found No_IP_Found No_IP_Found
No_IP_Found 84:d4:7e:cf:5c:9c No_IP_Found
CodePudding user response:
Assumptions:
- if both files contain an (ipv4) ip address then print the one from
file1
- both files have the same number of lines
- all lines contain a single field (with no embedded spaces); otherwise we only process the first space delimited field
One awk
idea:
awk '
BEGIN { regex="([0-9]{1,3}.){3}[0-9]{1,3}" } # define the (ipv4) ip address regex
FNR==NR { a[FNR]=$1; next } # 1st file: save field #1, skip to next input line
{ if (a[FNR] ~ regex) print a[FNR] # 2nd file: if 1st file had valid ip address then print it
else if ($1 ~ regex) print $1 # else if 2nd file has valid ip address then print it
else print "No_IP_Found" # else print default
}
' file1 file2
This generates:
192.168.217.36
192.168.229.70
192.168.197.181
192.168.230.81
No_IP_Found
No_IP_Found
CodePudding user response:
Using paste
because I'm lazy:
paste FILE1 FILE2 |
awk '
{
if (match($0,/[0-9]{1,3}(\.[0-9]{1,3}){3}/))
print substr($0,RSTART,RLENGTH)
else
print "No_IP_Found"
}
'
192.168.217.36
192.168.229.70
192.168.197.181
192.168.230.81
No_IP_Found
No_IP_Found
CodePudding user response:
awk '{$1~/\./?$0=$1:($2~/\./?$0=$2:$0="NO_IP_Found")}1' <(paste FILE1 FILE2)
CodePudding user response:
if you don't care about sorted order, here's a strange way to go it that filters out either ipv6 or MAC addresses but doesn't verify the ipv4 ones :
{m,g}awk '___[$_] <(NF*=NF==!_) || (__==$_)==(FNR-___[__])' \
__='No_IP_Found' FS=':' file1 file2
192.168.217.36
192.168.229.70
No_IP_Found
192.168.230.81
192.168.197.181
No_IP_Found