'select subdomains using print command
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
Solution 1:[1]
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
Solution 2:[2]
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
Solution 3:[3]
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
Solution 4:[4]
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?)
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RavinderSingh13 |
Solution 2 | anubhava |
Solution 3 | |
Solution 4 | dan |
Solution 5 | mivk |