简体   繁体   English

包含奇数管道字符的行的正则表达式

[英]Regex for lines containing an odd number of pipe characters

I'm cleaning up a LaTeX file, and I'm in a situation where I need to distinguish absolute value |x|我正在清理一个 LaTeX 文件,我遇到了需要区分绝对值 |x| 的情况。 from the set "such that" symbol ie {x |从集合“这样的”符号即 {x | x < 0}. x < 0}。

The first step for me is to find all lines containing an odd number of |我的第一步是找到所有包含奇数 | 的行。 characters (ie the pipe symbol).字符(即管道符号)。

In principle, I know how to do this, but I've tried the following regex command with no luck.原则上,我知道如何执行此操作,但是我尝试了以下正则表达式命令但没有成功。

egrep '^[^\|]*\|([^\|]*\|[^\|]*\|)*[^\|]*$'

The idea is that a matching line contains, in order:这个想法是匹配的行按顺序包含:

  • The line start线开始
  • 0 or more non-pipe characters 0 个或多个非管道字符
  • Exactly one pipe character一个管道字符
  • 0 or more copies of text containing exactly 2 pipes 0 个或多个文本副本,恰好包含 2 个管道
  • The line end线端

However, for some reason this isn't working.但是,由于某种原因,这不起作用。 I run the command on the following file:我在以下文件上运行命令:

\[
S = \{ x | x < 0}
y = |x|
\]

and none of the lines match.并且没有任何行匹配。

I suspect I'm making a silly mistake somewhere, possibly to do with escaping the pipe characters, but I'm stumped as to what's wrong.我怀疑我在某处犯了一个愚蠢的错误,可能与逃避管道字符有关,但我对出了什么问题感到困惑。

Can anybody tell me either how to fix this, or provide an alternate expression which matches lines containing an odd number of pipe characters?谁能告诉我如何解决这个问题,或者提供一个替代表达式来匹配包含奇数个管道字符的行?

Inside the [] , | []| is not a special character so should not be escaped by \\ . 不是特殊字符,因此不能被\\转义。 Try: 尝试:

egrep '^[^|]*\|([^|]*\|[^|]*\|)*[^|]*$'

Better to use awk for this purpose: 为此,最好使用awk:

awk -F '|' '!(NF%2)'

TESTING: 测试:

echo "a|bc|d|erg" | awk -F '|' '!(NF%2)'

OUTPUT: 输出:

a|bc|d|erg

echo "abc|d|ergxy" | awk -F '|' '!(NF%2)'

OUTPUT: 输出:

how about: 怎么样:

awk -F'|' 'NF&&(NF-1)%2' file

example: 例:

kent$  cat file
|foo|bar
| | | | |
||||||
|||||||

kent$  awk -F'|' 'NF&&(NF-1)%2' file
| | | | |
|||||||

Perl, which is cross platform (Windows too) and generally installed everywhere these days, is my axe of choice: Perl是跨平台的(Windows也是如此),如今通常安装在任何地方,是我的选择:

perl -ne 'print if (s/\\|/\\|/g) %2 == 1' file perl -ne'打印if(s / \\ | // || / g)%2 == 1'文件

script.sed脚本文件

#!/bin/sed -nf

# Save to hold
h

# Delete all non | chars
s@[^|]@@g

# Odd match
/^\(||\)*|$/ {
  # Fetch hold
  g
  s@^@odd\t:@
}

# Even match
/^\(||\)\+$/ {
  # Fetch hold
  g
  s@^@even\t:@
}

# No match
/^$/ {
  # Fetch hold
  g
  s@^@none\t:@
}

# Print
p

data.txt数据.txt

do|odd
do|odd|match|me
|even match|me
do|even match|me
do|even match|also|me|please

no-match

shell

sed -nf script.sed data.txt

stdout标准输出

odd :do|odd
odd :do|odd|match|me
even    :|even match|me
even    :do|even match|me
even    :do|even match|also|me|please
none    :
none    :no-match

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM