[英]grep to exclude a symbol in the beginning
I have an xml file and it has '<' in between the lines without escape characters in front.. So first thing i tried to parse the xml using: 我有一个xml文件,并且两行之间有'<',前面没有转义字符。.因此,我首先尝试使用以下方法解析xml:
xmllint --noout filename.xml
but that doesnt work.. because my xml version is 1.1 which is not supported.. So as an alternate I started searching for '<' excluding the beginning or the end of the sentence.. 但这不起作用..因为我的XML版本是1.1,不支持。.因此,作为替代,我开始搜索'<',不包括句子的开头或结尾。
should be fairly easy.. i tried: 应该相当容易..我试过了:
grep -v '^[<]'
but that is not working.. can someone help? 但这不起作用..有人可以帮忙吗?
ex: filename has: 例如:文件名具有:
<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>
I want output to be 我希望输出是
<instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
<field fieldname="CUR007" value="<EUR>"/>
Search for a <
or >
other than the first/last non-whitespace char which should be angle brackets. 搜索除第一个/最后一个非空白字符之外的<
或>
,该字符应为尖括号。
grep '^\s*<.*[<>].*>\s*'
Note that this matches the whole line, so it may be used if you are wanting to do something with the line (rather than just part of it). 请注意,这与整行匹配,因此,如果您想对该行进行某些操作(而不仅仅是其中一部分),可以使用它。
A test: 一个测试:
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
Output: 输出:
<instrument F001="6-A-1046" INSTRUMENT_ID="<xyz>" >
<field fieldname="CUR007" value="<EUR>"/>
I've created a different sample to add some more cases 我创建了另一个示例来添加更多案例
$ 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
[:blank:]
represents space and tab characters [:blank:]
代表空格和制表符 [^[:blank:]]
will match a non-blank character 因此[^[:blank:]]
将与非空白字符匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.