Cutting Part of Output

Hello all

I'm using bourne shell and need to figure out how to cut out a specific portion of some output. For example, my output from my command is:

12.12.52.125.in-addr.arpa name = hostname.domain.main.gov

I need to get just the "hostname.domain.main.gov" part. What I'm trying to do is make files named the "domain" part and putting the hostnames within them. I'm having a bit of trouble cutting out the "hostname" and "domain" part specifically though...

Any suggestions?

Thanks in advance, folks

man cut

So if I used -d".", how could I get, say the 5th or 6th part?

echo "12.12.52.125.in-addr.arpa name = hostname.domain.main.gov" | awk -F"=" 
'{print $2}'
1 Like
echo "12.12.52.125.in-addr.arpa name = hostname.domain.main.gov" | nawk -F'[ =.]' '{close(out);out=$(NF-2);print $(NF-3)>>out}'
1 Like

Was confused at the nawk for a sec then realized I should be using gawk :stuck_out_tongue:

Thanks for all your help guys. I really want to understand why these work though, so could someone explain the '{close(out);out=$(NF-2);print $(NF-3)>>out}' part if its not too much of a hassle?

So gawk -F is using "=" as a delimiter and then splitting up the output that way? then I assume the parts are assigned to $# variables then the $(NF-2 or 3) is getting the 'hostname' and 'domain' parts?

cut -d '.' -f 6

-F '[ =.]' makes space/=/. as field separators.
NF - NumberofFields

12.12.52.125.in-addr.arpa name = hostname.domain.main.gov

'gov' - last field (NF)
'main' - next to last field (NF-1)
'domain' - 2nd to last field (NF-2)
'hostname' - 3rd to last field (NF-3)

1 Like
echo "12.12.52.125.in-addr.arpa            name = hostname.domain.main.gov" | sed 's/.* = //'
$ a="12.12.52.125.in-addr.arpa               name = hostname.domain.main.gov"
$ echo "${a##* = }"
hostname.domain.main.gov
$
read l ; echo "${l##* }"