(致 LaTeX 用户)我想搜索手动标记的项目
(可能涉及的人) script file on GitHub
我试图找到解决方案,但我发现建议先删除空格。就我而言,我认为应该有更简单的解决方案。它可以使用 grep
或 awk
或其他一些工具。
考虑以下几行:
\item[a)] some text
\item [i) ] any text
\item[ i)] foo and faa
\item [ 1) ] foo again
我想查找(或计数)括号内是否有单个 )
的项目。该格式可以在括号内和/或周围有空格。此外,右括号之前的字符可以是任何字母或数字。
编辑:我尝试了 grep "\[a)\]"
但它错过了 [ a) ]
。
由于编写一个项目有很多可能的方法,因此我无法决定可能的模式。我认为这对我来说就足够了
\item<blank spaces>[<blank spaces><letter or number>)<blank spaces>]
替换空格不起作用,因为上面的模式通常包含其周围的文本(例如:\item[ a)] 考虑函数...
)
输出应指示是否存在此类模式。它可以是零或出现的次数。
最佳答案
因此,要在 grep
本身中完成这一切:
grep -c -E '\\item\s*\[\s*\w+\)\s*\]' file.txt
注意所有 \s*
检查空格。还有 -c
来获取计数。
分解:
\\ a backslash (needs escape in grep)
item "item"
\s* optional whitespaces
\[ "[" (needs escape in -E)
\s* optional whitespaces
\w+ at least one 'word' char
\) ")" (needs escape in -E)
\s* optional whitespaces
\] "]" (needs escape in -E)
关于bash - 忽略括号内/周围的空格来计算出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50648833/