Print specific words/numbers via grep/cut commands

15,681

Solution 1

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

Solution 2

a sed solution (works whether the field position of the version string is consistent or not, but the form of the version string must be consistent)

$ sed -r 's#.*/(.*)/([0-9]{2}\.[0-9]{2}\.[0-9]{2}).*#\1 \2#' test.txt
egg 12.34.56

joker 23.45.67

Explanation

  • -r use ERE so we don't have to escape () metacharacters
  • s#old#new# find pattern old and replace with new
  • .* match any or no characters in this position
  • (stuff) remember stuff for later
  • [0-9]{2}\.[0-9]{2}\.[0-9]{2} a pattern of [2 digits].[2 digits].[2 digits]
  • \1 \2 the two remembered patterns with a space in between

Solution 3

This will do it:

cut -d'/' -f5-6 test.txt | tr -s "/" " "

Solution 4

If you deal with fixed word positions this would work:

grep -e 'service_name[0-9]*=' test.txt|awk -F'/' '{ print $5" "$6 }'

Solution 5

$sed -r 's/.*\/([[:alpha:]]+)\/([\.0-9]*)\/.*/\1 \2/' test.txt

Explanation:

  • .* at the beginning and end cuts all characters that do not match the following
  • ([[:alpha:]]+) first subgroup in brackets matches only alphabetical characters
  • \/ matches a slash which will be cut out
  • next subgroup ([\.0-9]*) matches numbers and points and stores it in the second register
  • after the single slash / comes the substitution with \1 \2 inserts the first and second register from the matched subgroups
Share:
15,681

Related videos on Youtube

Donald
Author by

Donald

Updated on September 18, 2022

Comments

  • Donald
    Donald over 1 year

    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.

    • SeDav
      SeDav over 7 years
      is it necessary to do it with grep? I'd use sed or awk