cbcp
cbcp

Reputation: 339

Grabbing specific sections of text from a string

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

Upvotes: 0

Views: 136

Answers (2)

Mirage
Mirage

Reputation: 31568

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

Upvotes: 1

Chris Seymour
Chris Seymour

Reputation: 85913

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

Upvotes: 2

Related Questions