简体   繁体   English

句子中的Sed或Awk或Perl替换

[英]Sed or Awk or Perl substitution in a sentence

I need to make a substitution using Sed or other program. 我需要使用Sed或其他程序进行替换。 I have these patterns <ehh> 我有这些模式<ehh> <mmm> <mhh> repeated at the beginning of a sentences and I need to substitute for nothing. 在句子开头重复一遍,我不需要替代任何内容。

I am trying this: 我正在尝试:

echo "$line" | sed 's/<[a-zA-z]+>//g'

But I get the same result, nothing changes. 但是我得到了相同的结果,没有任何变化。 Anyone can help? 有人可以帮忙吗?

Thank you! 谢谢!

For me, for the test file 对我来说,对于测试文件

<ahh> test
<mmm>test 1

the following 下列

sed 's/^<[a-zA-Z]\+>//g' testfile

produces 产生

 test
test 1

which seems to be what you want. 这似乎是您想要的。 Note that for basic regular expressions, you use \\+ whereas for extended regular expressions, you use + (and need to use the -r switch for sed). 请注意,对于基本正则表达式,您使用\\+而对于扩展正则表达式,您使用+ (并且需要使用-r开关进行sed)。

NB: I added a ^ to the check since you said: at the beginning of the line. 注意:自从您说:在行的开头,我在支票上加上了^

echo '<ehh> <mmm> <mhh>blabla bla' | \
sed '^Js/^\([[:space:]]*\<[a-zA-Z]\{3\}\>\)\{1,\}//'
  • remove all starting occurence of your pattern (including heading space) 删除模式中所有开始出现的内容(包括标题空间)
  • I escape & to be sure due to sed meaning of this character in pattern (work without on my AIX) 我逃脱了&并确保由于此字符在模式中的sed含义(我的AIX上没有工作)
  • I don't use g because it remove several occurence of full pattern and there is only 1 begin ( ^ ) and use a multi occurence counter with group instead \\(\\)\\{1,\\} 我不使用g因为它删除了几次完整模式的出现,并且只有1个开始( ^ )并使用带有组的多出现计数器代替\\(\\)\\{1,\\}

If the goal is to get the last parameter from lines like this: 如果目标是从像这样的行中获取最后一个参数:

<ahh> test
<mmm>test 1

You can do: 你可以做:

awk -F\; '/^&lt;[[:alpha:]]+&gt/ {print $NF}' <<< "$line"
 test
test 1

It will search for pattern &lt;[[:alpha:]]+&gt and print last field on line, separated by ; 它将搜索模式&lt;[[:alpha:]]+&gt并在线打印最后一个字段,并用;分隔;

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

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