Combine/omit data from 2 files

i made a script on my own. this is for the inventory to all of my AWS servers, and i run it to all of my servers to get the hostname, please look at file2. Then i need some data in file3 as well,. i need to combine them

#cat file1
192.10.1.41
server.age.com
######

192.10.0.40
ssh cant connect
######

192.1.1.49 
server2.age.com
##########


#cat file 2
data3 ip.192.10.1.41.us stop 
data2 ip.192.10.0.40.internal running
data1 ip.192.1.1.49.internal idle      max




Output:
192.10.1.41  server.age.com  data3 stop
192.10.0.40  		     data2 running
192.1.1.49   server2.age.com data1 idle

What have you tried so far, and what/where is file2?

This what i tried. for file 2

cat file2|egrep -v '(Name|Desc)'|awk '{print $2,$1,$3}'

but you know its two files.

Hello,

Following may help you. Lets say input files are test13 and test12.
File test13:

cat test13
data3 192.10.1.41 stop 
data2 192.10.0.40 running
data1 192.1.1.49 idle      max

File test12:

cat test12
192.10.1.41
server.age.com
######

192.10.0.40
ssh cant connect
######

192.1.1.49 
server2.age.com
##########

Code is as follows.

awk 'FNR==NR{A[$2]=$1 OFS $3;next} ($1 in A){D=A[$1];C=$1;getline; {if($0 !~ /ssh cant/) {C=C OFS $0} else {C=C "\t"}};$0=C OFS D;print $0}' test13 test12 

Output will be as follows.

awk 'FNR==NR{A[$2]=$1 OFS $3;next} ($1 in A){D=A[$1];C=$1;getline; {if($0 !~ /ssh cant/) {C=C OFS $0} else {C=C "\t"}};$0=C OFS D;print $0}' test13 test12
192.10.1.41 server.age.com data3 stop
192.10.0.40     data2 running
192.1.1.49 server2.age.com data1 idle

Thanks,
R. Singh

Above was jsut example..

cat g|egrep -v '(Name|Desc)'|awk '{print $1,$3,$4}'|sed s/.us.east.2.compute.internal//|sed s/ip.//


us.east.2c i.1111111 192.26.97.70    (IM HOPING TO PUT THE HOSTNAME HERE)
us.east.2b i.222222 192.26.93.41      (HOSTNAME HERE)

Please don't modify posts that have been referred to by others! And. please apply sufficient painstakingness when writing your spec. Now it is referring to file3 which is not present anymore.

Sing lets make it dynamic

something along these lines: nawk -f ken.awk OFS='\t' file1.txt file2.txt , where ken.awk is:

FNR==NR {
   if (NF==1) {
    if (/^[0-9.]+$/)
       ip=$1
    if (/[a-zA-Z.]$/)
       name[ip]=$1
   }
   next
}
NF {
  ip=substr($2,index($2,".")+1)
  gsub("[.][^0-9]+$","",ip)
  print ip, name[ip], $1, $3
}
1 Like

Kenshinhimura:

You have been reminded to use code tags on so many occasions you are on the verge of being automatically tempbanned for having so many warnings. This would have already happened, if the unix.com forum software hadn't reminded me you were close to the limit. Please use code tags!

If you are confused on the process, view this video we have prepared for your convenience.

If you can't play the video, or the button doesn't work in your browser or phone, then type them yourself -- like this.

```text
stuff
```

good script...thanks all..sorry mods..
but the script is not dynamic..what if..tehre is no ip.1.1.1.1.blah..

only 1.1.1.1 it will omit..

but thanks

---------- Post updated at 04:13 PM ---------- Previous update was at 04:12 PM ----------

Team,
you may use the commands given by our genuises here, to all admins that has large inventory of servers..awk is very powerful

What does "not dynamic" mean exactly?
Could you post a representative sample to capture all the cases and the desired output, please?!

---------- Post updated at 05:22 PM ---------- Previous update was at 05:17 PM ----------

I think this is what you're after:

FNR==NR {
   if (NF==1) {
    if (/^[0-9.]+$/)
       ip=$1
    if (/[a-zA-Z.]$/)
       name[ip]=$1
    }
   next
}
NF {
  ip=substr($2,index($2,".")+1)
  gsub("[.][^0-9]+$","",ip)
  print ip, name[ip], $1, $3
  delete name[ip]
}
END {
  for (i in name)
     print i, name
}