Reputation: 44618
I have a file containing (one per line). I would like to extract word between quotes starting with the some pattern. (in my case it is C_)
"PATTERNabcde" sdfds sdfds
"sdfsdfsdf" sdfdsf sdfdsf
" PATTERNabc" dfdsdfd
and I want to extract: PATTERNabcde
PATTERNabc
EDIT:
I would like to ALSO extract word between quotes that don't start with the $PATTERN
.
Upvotes: 0
Views: 4724
Reputation: 4675
You can use awk:
awk -F\" '$2~/^[[:space:]]*PATTERN/{print $2}' file
This works if there is only one word enclosed in quotation marks or if the one that may start with pattern is always the first one; otherwise, you'd have to use a for cycle:
awk -F\" '{for (i=2;i<=NF;i+=2) if ($i ~/^[[:space:]]*PATTERN/ ) {print $i;next}}'
Upvotes: 2
Reputation: 455342
You can use grep
as:
grep -Po '(?<=")\s*PATTERN[^"]*(?=")' file
Upvotes: 2
Reputation: 284927
sed -rn 's/.*?".*?(PATTERN[^"]*)".*/\1/p'
-r
- extended regex-n
- disabled auto-print.*?
- zero or more characters, non-greedy(
- open capturing group[^"]
- any character but "
)
- close capturing group\1
- first matching groupp
- printWe just replace every line with the first group. If there is a replacement, we print.
Upvotes: 2