在 Bash 中提取单词后的字符串文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42052154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:43:15  来源:igfitidea点击:

Extract the text of string after a word in Bash

bashawksed

提问by Rohit Sahu

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

如何在像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 cvtcommand and I want to get the text after "Modeline".

cvt命令的典型输出,我想在“Modeline”之后获取文本。

回答by karakfa

other variations

其他变体

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

or

或者

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

回答by SLePort

With sed, using backreference:

使用 sed,使用反向引用:

$ sed 's/.*Modeline\(.*\)//' <<< '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

回答by Inian

Using bashparameter-expansion,

使用bash参数扩展,

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