Search code examples
bashawksedusingcut

how to use cut command -f flag as reverse


This is a text file called a.txt

ok.google.com  
abc.google.com

I want to select every subdomain separately
cat a.txt | cut -d "." -f1 (it select ok From left side)
cat a.txt | cut -d "." -f2 (it select google from left side)

Is there any way, so I can get result from right side
cat a.txt | cut (so it can select com From right side)


Solution

  • There could be few ways to do this, one way which I could think of right now could be using rev + cut + rev solution. Which will reverse the input by rev command and then set field separator as . and print fields as per they are from left to right(but actually they are reversed because of the use of rev), then pass this output to rev again to get it in its actual order.

    rev Input_file | cut -d'.' -f 1 | rev