Home > other >  Could anyone of you please let me know, In shell Scripting if Input =Prefixname_SuffixName-123, how
Could anyone of you please let me know, In shell Scripting if Input =Prefixname_SuffixName-123, how

Time:12-06

Could anyone of you please let me know, In shell Scripting if Input =Prefixname_SuffixName-123, how to get only the part after '_' as new Input ?

Input = Prefixname_SuffixName-123

Expected should be, New_input = SuffixName-123

Note: The prefixname is not fixed length string, it varies all the time.

I have tried,

new_name=cat $input | awk '{print $1}' | cut -d="_"

error : cut: expected a list of bytes, characters, or fields

CodePudding user response:

Using awk:

new_name=$(echo "$Input" | awk -F_ '{print $2}')

and if you were using bash instead of sh you could do

Input='Prefixname_SuffixName-123'
new_name=${Input#*_}
echo "$new_name"
SuffixName-123
  • Related