bash - Regex to extract/output quoted strings from a file -
i wrote simple regular expression output quoted strings file
cat mobydick.txt | while read line; echo -n "$line "; done | grep -op '[^"]*"\k[^"]*'
this have far
for example, when run one-liner on file mobydick.txt output in single line instead of new line separated strings.
could me script?
expected output --> when above script run on mobydick.txt
"from twenty-fifth year date life."
"call me ishmael."
above input file can downloaded url
using gnu grep(1) (other incarnations of grep(1) don't have -p
):
tr '\n' ' ' <mobydick.txt | grep -p -o '(?<=\s)"[^"]+"(?=\s)'
more accurate, using pcregrep(1):
pcregrep -m -o '(?<=^|\s)"[^"]+"(?=$|\s)' mobydick.txt
Comments
Post a Comment