• Uncategorized

About linux : Select-subdomains-using-print-command

Question Detail

cat a.txt

a.b.c.d.e.google.com
x.y.z.google.com
rev a.txt | awk -F. '{print $2,$3}' | rev

This is showing:

e google
x google

But I want this output

a.b.c.d.e.google
b.c.d.e.google
c.d.e.google
e.google
x.y.z.google
y.z.google
z.google

Question Answer

With your shown samples, please try following awk code. Written and tested in GNU awk should work in any awk.

awk '
BEGIN{
  FS=OFS="."
}
{
  nf=NF
  for(i=1;i<(nf-1);i++){
    print
    $1=""
    sub(/^[[:space:]]*\./,"")
  }
}
' Input_file

Here is one more awk solution:

awk -F. '{while (!/^[^.]+\.[^.]+$/) {print; sub(/^[^.]+\./, "")}}' file

a.b.c.d.e.google.com
b.c.d.e.google.com
c.d.e.google.com
d.e.google.com
e.google.com
x.y.z.google.com
y.z.google.com
z.google.com

Using sed

$ sed -En 'p;:a;s/[^.]+\.(.*([^.]+\.){2}[[:alpha:]]+$)/\1/p;ta' input_file
a.b.c.d.e.google.com
b.c.d.e.google.com
c.d.e.google.com
d.e.google.com
e.google.com
x.y.z.google.com
y.z.google.com
z.google.com

Using bash:

IFS=.
while read -ra a; do
    for ((i=${#a[@]}; i>2; i--)); do
        echo "${a[*]: -i}"
    done
done < a.txt

Gives:

a.b.c.d.e.google.com
b.c.d.e.google.com
c.d.e.google.com
d.e.google.com
e.google.com
x.y.z.google.com
y.z.google.com
z.google.com

(I assume the lack of d.e.google.com in your expected output is typo?)

For a shorter and arguably simpler solution, you could use Perl.

To auto-split the line on the dot character into the @F array, and then print the range you want:

perl -F'\.' -le 'print join(".", @F[0..$#F-1])' a.txt

-F'\.' will auto-split each input line into the @F array. It will split on the given regular expression, so the dot needs to be escaped to be taken literally.

$#F is the number of elements in the array. So @F[0..$#F-1] is the range of elements from the first one ($F[0]) to the penultimate one. If you wanted to leave out both “google” and “com”, you would use @F[0..$#F-2] etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.