How to use cat grep and cut together?

Hello,

I try to

#!/bin/bash

for ip in $(seq 1 255  );do
    host 10.11.1.$ip >> nameserver2.txt
done

I'm trying to clean up a file I created with this little batch script.

I have more results with names of servers found and not found.
Host 1.1.11.10.in-addr.arpa. not found: 3 (NXDOMAIN)
Host 2.1.11.10.in-addr.arpa. not found: 3 (NXDOMAIN)

I'm doing a nameserver2.txt grep | grep name which displays only existing servers. Except that I have too much information on each line
5.1.11.10.in-addr.arpa domain name pointer serv1.domain.com
6.1.11.10.in-addr.arpa domain name pointer serv2.domain.com

I would like to have only the 1st character each line (so the last of each IP and the name of the server
I'm making a

cat nameserver2.txt | grep name | cut -d. -f1

but in this case I only have information on two so in the end I made this order:

cat nameserver.txt | grep name | cut -d. -f1.6 | cut -d '' -f1.5

which therefore results:

5.arpa srv1
6.arpa srv2

I can't delete .arpa.

How can I do ?

awk '/name/ {sub("[.].*", "", $1); sub("[.].*", "", $NF); print $1, $NF}' nameserver.txt

Not sure what either file's contents is. For the original file, try

awk  '! /^Host/ {split ($1, T1, "\."); split ($NF, T2, "\."); print T1[1], T2[1]}' nameserver2.txt
1 Like

Thank you for being so quick and fair in your answers

The 1st proposition works perfectly. the second almost except that for the servers not found, there is this:
;; reached
( you can send me a other command line if you want )

in any case to you two

just for knowledge, could it have been done with cat grep and cut?

grep name nameserver.txt | cut -d" " -f1,5 --output-delimiter="." | cut -d"." -f1,7 --output-delimiter=" "
1 Like

There's NO string like "reached" in your samples, nor semicolons. Those don't show up in my host calls, either. Replace ! /^Host/ with /domain/ or /name/ or /pointer/ . If that doesn't help, show your input data.

cat is by no means needed in any of your attempts as both grep and cut can open files by themselves, or you can redirect stdin for them.
grep ping the first number is quite easy, like

$ host 82.165.230.17 | grep -o "^[^.]*"
17

, but extracting the remote hostname not so as you don't know what the FQDN looks like. The nearest I get is

$ host 82.165.230.17 | grep -oE "^[^.]*| [^. ]*\."
17
 bap.

Yes you are right. it's my fault. There somes lines that i didn't see in the original file, like ";; connection timed out; no servers could be reached"

thank you for your explanation and your help.