Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk command to extract string after a specific pattern string

Tags:

shell

awk

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"
like image 985
harini k Avatar asked Mar 29 '26 00:03

harini k


1 Answers

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:

like image 199
Allan Avatar answered Apr 02 '26 11:04

Allan