harini k
harini k

Reputation: 43

awk command to extract string after a specific pattern string

I want to extract the text after the pattern "List values are here:" that are in quotes in a list. I'm very new to this. Can someone please assist

List values are here: "list1 abc" "list2 test" "end of list"

What I have done:

echo $va| awk '/List values are here:/ {print $1}' var="$va"

Upvotes: 3

Views: 5926

Answers (2)

Ed Morton
Ed Morton

Reputation: 204488

$ echo "${val#*: }"
"list1 abc" "list2 test" "end of list"

Upvotes: 1

Allan
Allan

Reputation: 12456

You do not need to use awk or sed for this kind of task since you only need to fetch some part of a line. grep is the tool you are looking for.

$ grep -oP '(?<=List values are here: ).*'

EXAMPLE:

$ echo 'List values are here: "list1 abc" "list2 test" "end of list"' | grep -oP '(?<=List values are here: ).*'
"list1 abc" "list2 test" "end of list"

after you can assign the result to a variable or do whatever you want with it.

Explanations: - -o is to change the default behavior of grep which is outputting the whole line to outputting only the pattern - -P is to use perl regex - (?<=List values are here: ).* regex to fetch everything after List values are here:

Upvotes: 3

Related Questions