Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex grep以排除开头的符号_Regex_Unix_Grep - Fatal编程技术网

Regex grep以排除开头的符号

Regex grep以排除开头的符号,regex,unix,grep,Regex,Unix,Grep,我有一个xml文件,它有“搜索一个,而不是第一个/最后一个非空白字符,它应该是尖括号 grep '^\s*<.*[<>].*>\s*' grep'^\s*\s*' 请注意,这与整行匹配,因此,如果您希望对该行(而不仅仅是其中的一部分)执行某些操作,则可以使用它 测试: grep '^\s*<.*[<>].*>\s*' << EOF > <instrument F001="6-A-1046" INSTRUMENT_I

我有一个xml文件,它有“搜索一个
,而不是第一个/最后一个非空白字符,它应该是尖括号

grep '^\s*<.*[<>].*>\s*' 
grep'^\s*\s*'
请注意,这与整行匹配,因此,如果您希望对该行(而不仅仅是其中的一部分)执行某些操作,则可以使用它


测试:

grep '^\s*<.*[<>].*>\s*' << EOF
>  <instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
>   <field fieldname="CUR007" value="<EUR>"/>
>   <field fieldname="C207" value="2023-01-11"/>
>   <field fieldname="INS160" value="0"/>
>   <field fieldname="PRD013" value="1020"/>
>   <field fieldname="PRD150" value="0"/>
>   <field fieldname="PRD205" value="0"/>
>  </instrument>
> EOF
grep'^\s*\s*'
>   
>   
>   
>   
>   
>   
>  
>EOF
输出:

<instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
 <field fieldname="CUR007" value="<EUR>"/>

我创建了一个不同的示例来添加更多的案例

$ cat ip.txt
foo bar < xyz
<123 abc <42> >
  <good>
bad > line

$ # get lines having < not at start of line
$ grep '[^[:blank:]].*<' ip.txt
foo bar < xyz
<123 abc <42> >

$ # get lines having > not at end of line
$ grep '>.*[^[:blank:]]' ip.txt
<123 abc <42> >
bad > line

$ # combining the two
$ grep -E '[^[:blank:]].*<|>.*[^[:blank:]]' ip.txt
foo bar < xyz
<123 abc <42> >
bad > line
$cat ip.txt
foo-bar行
$#获取行开头有<的行
$grep'[^[:blank:][]..*[^[:blank:][]'ip.txt
坏>行
$#将两者结合起来
$grep-E'[^[:blank:][]..*.[^[:blank:][]'ip.txt
foo-bar
  • [:blank::
    表示空格和制表符
  • 因此,
    [^[:blank:]
    将匹配一个非空字符

已编辑:)谢谢我无法理解你在这里需要的逻辑。。。你能再次解释为什么输出中只显示前两行吗?@sundeep因为有一个
“@thealchesman如果一行包含
foo bar
它应该是输出的一部分吗?@sundeep:if'
\s
不能被所有grep实现识别…可以使用
grep'^[:space:]*非常感谢@Bohemian..关于搜索“>”而不是在结尾..?@TheAlchesman我已经编辑了答案,以匹配任意一个尖括号,而不是它们应该在的位置。
grep '^\s*<.*[<>].*>\s*' 
grep '^\s*<.*[<>].*>\s*' << EOF
>  <instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
>   <field fieldname="CUR007" value="<EUR>"/>
>   <field fieldname="C207" value="2023-01-11"/>
>   <field fieldname="INS160" value="0"/>
>   <field fieldname="PRD013" value="1020"/>
>   <field fieldname="PRD150" value="0"/>
>   <field fieldname="PRD205" value="0"/>
>  </instrument>
> EOF
<instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
 <field fieldname="CUR007" value="<EUR>"/>
$ cat ip.txt
foo bar < xyz
<123 abc <42> >
  <good>
bad > line

$ # get lines having < not at start of line
$ grep '[^[:blank:]].*<' ip.txt
foo bar < xyz
<123 abc <42> >

$ # get lines having > not at end of line
$ grep '>.*[^[:blank:]]' ip.txt
<123 abc <42> >
bad > line

$ # combining the two
$ grep -E '[^[:blank:]].*<|>.*[^[:blank:]]' ip.txt
foo bar < xyz
<123 abc <42> >
bad > line