开发者

sed + cut words before the first “END” word in line

开发者 https://www.devze.com 2023-01-07 07:08 出处:网络
How to cut words before the first “END” word in line, and not include the first character before the “END” word ( in our examples the character is “-“)

How to cut words before the first “END” word in line, and not include the first character before the “END” word ( in our examples the character is “-“)

Only with sed command

For example:

  echo AAA-BBB-CCC-END | sed ...
  AAA-BBB-CCC


  echo 1-END-3-4 | sed ...
  1     


  echo 1-2-END-3-4 | sed ...
  1-2


  echo PARAM1-PARAM2-END | sed ...
  PARAM1-PARAM2


  echo fileExample1-fileExample2-END | sed ...
  fileExample1-fileE开发者_如何转开发xample2


  echo xyz_123-END | sed ...
  xyz_123


sed "s/.END.*//"

This should do the trick.

It'll remove the first char before the END, and all chars after (and of course the END itself).


sed -r "s/(.*).END.*/\1/"

would be my suggestion


if your string structure always have hyphens and then the "END" word (eg -END),

$ var="AAA-BBB-CCC-SEND-END"
$ echo ${var%%-END*}
AAA-BBB-CCC-SEND

As you can see, in case you have words like "SEND", it will still give you the correct output.

if you still prefer using sed,

$ echo $var | sed 's/-END.*//'
AAA-BBB-CCC-SEND


this seems to work: awkish not sedish, & literal

awk '$0 !~ /(echo*|^$)/'

0

精彩评论

暂无评论...
验证码 换一张
取 消