简体   繁体   中英

Sed or Awk or Perl substitution in a sentence

I need to make a substitution using Sed or other program. I have these patterns <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).

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)
  • 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,\\}

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 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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