Ubuntu – Print specific words/numbers via grep/cut commands

command linegreptext processing

I have a file test.txt, which contains the following result:

service_name1= apple/ball/cat/dog/egg/12.34.56/ball/apple
service_name2= fan/girl/house/ice/joker/23.45.67/fan/girl

and so on up to service_name1500

I want output like :

egg 12.34.56
joker 23.45.67

and so on: the version number along with the word before that.

Best Answer

This should be a simple cut job:

cut -d/ -f5,6 --output-delimiter=" "
  • -d/ sets the input delimiter as /
  • -f5,6 outputs only the 5th and 6th field
  • --output-delimiter=" " sets the output delimiter as a space

Same thing with awk, awk by default sets the output field separator as space:

awk -F/ '{print $5,$6}'

Example:

% cat file.txt
service_name1= apple/ball/cat/dog/egg/12.34.56/ball/apple
service_name2= fan/girl/house/ice/joker/23.45.67/fan/girl

% cut -d/ -f5,6 --output-delimiter=" " file.txt
egg 12.34.56
joker 23.45.67

% awk -F/ '{print $5,$6}' file.txt
egg 12.34.56 
joker 23.45.67
Related Question