Danial Wayne
Danial Wayne

Reputation: 348

Extracting a string from a string in linux

Extract the value for OWNER in the following:

{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL [email protected]}}

To get this string I am using grep on a text file. In all other cases only one value is contained on each line, so they are not an issue. My problem is removing the text after OWNER but before the }} brackets, leaving me with only the string 'Wayne, Daniel'.

So far I have began looking into writing a for loop to go through the text a character at a time, but I feel there is a more elegant solution then my limited knowledge of unix.

Upvotes: 1

Views: 66

Answers (4)

Alan
Alan

Reputation: 185

Try this. I use cut

INPUT="{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL [email protected]}}"
SUBSTRING=`echo $INPUT| cut -d' ' -f3`
SUBSTRING2=`echo $INPUT| cut -d',' -f2`
SUBSTRING2=`echo $SUBSTRING2| cut -d'}' -f1`
echo $SUBSTRING$SUBSTRING2

maybe is not the most elegant way but works.

Upvotes: 1

Amit
Amit

Reputation: 20456

With grep

> cat file
{{USERID 9898}}{{OWNER Wayne, Daniel}}{{EMAIL [email protected]}}

> grep -Po '(?<=OWNER )[\w, ]*' file
Wayne, Daniel

Upvotes: 3

anubhava
anubhava

Reputation: 784968

You can use this awk:

awk -F '{{|}}' '{sub(/OWNER +/, "", $4); print $4}' file
Wayne, Daniel

Upvotes: 3

Lorenz
Lorenz

Reputation: 2259

Try cat file.txt | perl -n -e'/OWNER ([^\}]+)/ && print $1'

Upvotes: 3

Related Questions