Grabbing specific sections of text from a string

↘锁芯ラ 提交于 2020-01-11 12:52:09

问题


I've got a string that I need to grab two section out - they vary as it's a PHP language file. Hoping someone can help, the string is:

$_LANG['FIELD1'] = "FIELD2";

I need to grab FIELD1 and FIELD2


回答1:


This should do it:

# space seperated
$ sed -n "s/.*_LANG\['\([^']*\)'] = .\(\w*\).*/\1 \2/p" file
FIELD1 FIELD2

# newline seperated 
$ sed -n "s/.*_LANG\['\([^']*\)'] = .\(\w*\).*/\1\n\2/p" file
FIELD1
FIELD2

Or using grep with positive lookbehind:

$ grep -Po "(?<=_LANG\[')[^']*" file
FIELD1

$ grep -Po '(?<=_LANG\[.FIELD1.\] = ")[^"]*' file
FIELD2



回答2:


awk -F "[\"']" '{ print $2, $4 }' file


来源:https://stackoverflow.com/questions/14202619/grabbing-specific-sections-of-text-from-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!