Home > front end >  select subdomains using print command
select subdomains using print command

Time:04-11

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

Thank You in Advance

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

Using sed

$ sed -n '/^/p;:a;s/[a-z]*\.\(.*[a-z]\.[a-z]*\.[a-z]*\)/\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

CodePudding user response:

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?)

  • Related