1

Anyone familiar with how to use regex grab the following expression:

Random1 `json:"random1"`
Random2 `json:"random2"`
Random3 `json:"random3"`

desired output:

random1
random2
random3

What I have tried so far:

Try one:

grep -o '(\".*?\")' example1.file 

Try two:

grep -o '\"*\"' example1.file

returned nothing

This returns everything but just want the substring:

Attempt 3

grep -E "\"(.*)\"" example1.file 

Expected output:

random1
random2
random3
1
  • awk -F\" '{print $2}' file? Commented May 9, 2022 at 23:01

3 Answers 3

1

Using sed:

sed -E 's/^.*"(.*)"`$|.*/\1/g' example1.file

This (the |.* part) also clears lines without a match. Which then can be easily filtered out with ... | grep .

Sign up to request clarification or add additional context in comments.

1 Comment

Worked on Mac fine
1

You need to use -P with lookarounds, so that the quotes won't be included in the match.

grep -o -P '(?<").*(?=")' example1.file

This requires GNU grep to get the -P option for PCRE.

1 Comment

This works well on linux
1

With GNU grep and a Perl-compatible regular expression:

grep -Po '(?<=").*(?=")' file

Output:

random1
random2
random3

1 Comment

This works will on linux. For mac users make sure gnu grep with -P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.