Rohit Sahu
Rohit Sahu

Reputation: 123

Extract the text of string after a word in Bash

How can I extract the text after a word like Modeline.

I have a string,

mdline = 1600x900 59.95 Hz (CVT 1.44M9) hsync: 55.99 kHz; pclk: 118.25 MHz Modeline "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync

i.e a typical output of cvt command and I want to get the text after "Modeline".

Upvotes: 11

Views: 33228

Answers (3)

SLePort
SLePort

Reputation: 15461

With sed, using backreference:

$ sed 's/.*Modeline\(.*\)/\1/' <<< 'mdline = 1600x900 59.95 Hz (CVT 1.44M9) hsync: 55.99 kHz; pclk: 118.25 MHz Modeline "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync'
 "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync

Upvotes: 1

karakfa
karakfa

Reputation: 67467

other variations

$ awk -F'Modeline ' '{print $2}' file

or

$ sed 's/.*Modeline //' file

Upvotes: 20

Inian
Inian

Reputation: 85530

Using bash parameter-expansion,

string='mdline = 1600x900 59.95 Hz (CVT 1.44M9) hsync: 55.99 kHz; pclk: 118.25 MHz Modeline "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync'

printf "%s\n" "${string#*Modeline }"
"1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync

Upvotes: 5

Related Questions