I have a file containing (one per line). I would like to extract word between quotes starting with the some patte开发者_如何学Gorn. (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
.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}}'
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
- print
We just replace every line with the first group. If there is a replacement, we print.
You can use grep
as:
grep -Po '(?<=")\s*PATTERN[^"]*(?=")' file
See it
精彩评论