0

My data is like (a.txt)

allheartweb.com
whoisdatacenter.com
domainsanalytics.com
covercian.co.uk

I want to extract all TLD from there

My code is

cut -d "." -f 2 a.txt 

its show me

com
com
com
co

if I do

cut -d "." -f 2,3

it gave me

com
com
com
co.uk

I want

com
com
com
uk

Any idea how can I do that

1 Answers1

0

Use awk:

awk -F'.' '{ print $NF }' infile

The $ in awk is an operator that returns the content of the corresponding valid field.

fields in awk are splitting based on the FS (Field Seperator), which default is Tab(s)/Space(s) (Note: with default FS, all the leading/trailing whitespaces will be ignored too and if you wanted to capture them and so capture thr empty first/last fields then you will need to specify the FS as a regex type.). the FS can be changed with -F switch or via awk internal FS variable.

The NF in awk represent the last field, so by $NF we are telling to print last field's content based on the dot . as the FS.

αғsнιη
  • 41,407