Dave
Dave

Reputation: 17

How to grep with a pattern that includes a " quotation mark?

I want to grep a line that includes a quotation mark, more specifically I want to grep lines that include a " mark.

more specifically I want to grep lines like:

#include "something.h"
then pipe into sed to just return something.h

Upvotes: 1

Views: 1126

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed '#n
/"/ s/.*"\([^"]*\)" *$/\1/p' YourFile

No need of grep (unless performance on huge file is wanted) with a sed. Sed could filter and adapt directly the content

In your case, /"/ is certainly modified by /#include *"/

in case of several string between quote

sed '#n
/"/ {s/"[^"]*$/"/;s/[^"]*"\([^"]*\)" */\1/gp;}' YourFile

Upvotes: 1

anubhava
anubhava

Reputation: 785721

You can use awk to get included filename:

awk -F'"' '{print $2}' file.c
something.h

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174806

A single grep will do this job.

grep -oP '(?<=")[^"]*(?=")' file

Example:

$ echo '#include "something.h"' | grep -oP '(?<=")[^"]*(?=")'
something.h

Upvotes: 3

Related Questions