sed - How to get "all but the last N fields/things" in shell script? -
i encounter case need beginning of string without last part.
for example, host part of fqdn without domain name:
www.example.org -> www foo.bar.example.org -> foo.bar foo.bar.baz.some.domain.name -> foo.bar.baz.some (the same applies other examples need strip end of input.)
i use mix of cut , rev this, such as:
echo "foo.bar.example.org" | rev | cut -d "." -f 3- | rev this trick, wonder if there better way this?
with awk do:
$ awk '{nf=nf-2}1' fs=. ofs=. file www foo.bar foo.bar.baz.some generalize last n feilds n=2:
$ awk '{nf=nf-n}1' fs=. ofs=. n=2 file www foo.bar foo.bar.baz.some with sed do:
$ sed 's/[.][^.]*[.][^.]*$//' file www foo.bar foo.bar.baz.some generalized:
$ sed -r 's/([.][^.]*){2}$//' file www foo.bar foo.bar.baz.some
Comments
Post a Comment