当前位置: 动力学知识库 > 问答 > 编程问答 >

regex - How to stripoff the string after space, slash (/), greater than (>),dot(.) in shell

问题描述:

I need to strip-off the data which comes after space , slash /, greater than >, dot .. I only need to display the part before the space, slash, dot, greater than symbol in given string data using a shell or Perl script.

ex:

a15-ap-5035.wma.ibm.com

a15-ap-99995/wma.ibm.com

a15-ap-99995>wma.ibm.com

网友答案:

In shell you can do string manipulation:

s='a15-ap-99995>wma.ibm.com'
echo "${s/[,\/. >]*/}"
a15-ap-99995>wma

s='a15-ap-5035.wma.ibm.com'
echo "${s/[,\/. >]*/}"
a15-ap-5035

Using `perl:

perl -pe 's~[,/>. ].*~~' <<< "$s"
网友答案:

stripoff the data which comes after space, slash (/), greater than (>),(.),

In Perl it would be like,

$str =~ s/[\/\s>.].*//;
网友答案:

Using Bash Parameter Expansion:

s="a15-ap-5035.wma.ibm.com"
echo "${s%%[./> ]*}"
分享给朋友:
您可能感兴趣的文章:
随机阅读: