問題描述
如何使用 grep 查找單詞後兩個字符之間的所有內容,而不輸出整行? (How do I find everything between two characters after a word using grep, without outputting the entire line?)
I am downlading the info.0.json file from xkcd and trying to parse just the alt text. I don't care if there are quotes around it or not. The problem it that the info.0.json file is all one line, and the alt text is in quotes after the word "alt=". Trying cat info.0.json | grep alt
just returns the whole file (because it's all one line). What is the grep or sed code that will get me the alt text?
參考解法
方法 1:
Use the -o
switch:
-o, --only-matching show only the part of a line matching PATTERN
Example:
grep -o 'alt="[^"]*"'
(by Nick Sweeting、Mark Byers)