为什么 grep 仅在组织模式源代码块中部分工作?

问题描述 投票:0回答:1

这是一个关于清理 SPARQL 数据集的 后续问题

数据集是通过这个cell获得的:

#+name: raw-dataset
#+BEGIN_SRC sparql :url https://query.wikidata.org/sparql :format text/csv :cache yes :exports both
SELECT ?wLabel ?pLabel
WHERE
{
  ?p wdt:P31 wd:Q98270496 .
  ?p wdt:P1416 ?w .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ASC(?wLabel) ASC(?pLabel)
LIMIT 10
#+END_SRC


#+RESULTS[7981b64721a5ffc448aa7da773ce07ea8dbaf8ac]: raw-dataset
| wLabel                                        | pLabel       |
|-----------------------------------------------+--------------|
| Q105775472                                    | NFDI4Health  |
| Q1117007                                      | NFDI4Health  |
| Q115254989                                    | NFDI4Objects |
| Q1205424                                      | NFDI4Objects |
| Q17575706                                     | NFDI4Objects |
| Academy of Sciences and Humanities in Hamburg | Text+        |
| Academy of Sciences and Literature Mainz      | NFDI4Culture |
| Academy of Sciences and Literature Mainz      | NFDI4Memory  |
| Academy of Sciences and Literature Mainz      | NFDI4Objects |
| Academy of Sciences and Literature Mainz      | Text+        |

第二步,我使用

zsh
查看数据:

#+begin_src sh :var data=raw-dataset :shebang "#!/opt/homebrew/bin/zsh" :exports both
echo ${data}
#+end_src

#+RESULTS:
| Q105775472                                    | NFDI4Health  |
| Q1117007                                      | NFDI4Health  |
| Q115254989                                    | NFDI4Objects |
| Q1205424                                      | NFDI4Objects |
| Q17575706                                     | NFDI4Objects |
| Academy of Sciences and Humanities in Hamburg | Text+        |
| Academy of Sciences and Literature Mainz      | NFDI4Culture |
| Academy of Sciences and Literature Mainz      | NFDI4Memory  |
| Academy of Sciences and Literature Mainz      | NFDI4Objects |
| Academy of Sciences and Literature Mainz      | Text+        |

一切都好,我可以从清洁部分开始,去掉所有包含

Q....
:

的线条
#+begin_src sh :var data=raw-dataset :exports both :shebang "#!/opt/homebrew/bin/zsh"
echo ${data} | grep -L -E "Q[1-9]"
#+end_src

#+RESULTS:

不知何故,使用

-L
不起作用。 但是如果没有(
-L
),我会从代码中得到预期的结果:

#+begin_src sh :var data=raw-dataset :shebang "#!/opt/homebrew/bin/zsh" :exports both
echo ${data} | grep -E "Q[1-9]"
#+end_src

#+RESULTS:
| Q105775472 | NFDI4Health  |
| Q1117007   | NFDI4Health  |
| Q115254989 | NFDI4Objects |
| Q1205424   | NFDI4Objects |
| Q17575706  | NFDI4Objects |

问题:为什么

-L
不起作用以及如何摆脱以
Q....
开头的行?

grep org-mode
1个回答
0
投票

要排除以 Q 开头然后是数字的行:

  • ... | grep -v '^Q[0-9]'
  • ... | grep -Pv '^Q\d'

正如评论中提到的,

-L
是一个罕见的命令行选项(
--files-without-match
),用于排除整个文件。
-v
非常常见,用于排除与模式或模式匹配的线。
v
中提到的
man grep
来自
--invert-match

© www.soinside.com 2019 - 2024. All rights reserved.