How to get a substring after the last underscore (_) in unix shell script

后端 未结 3 1114
醉话见心
醉话见心 2021-01-19 18:00

I have a String like this

this_is_test_string1_22
this_is_also_test_string12_6

I wanted to split and extracts string around the last unders

3条回答
  •  忘掉有多难
    2021-01-19 18:30

    Using awk:

    $ awk 'BEGIN{FS=OFS="_"}{last=$NF;NF--;print $0" "last}' < this_is_test_string1_22
    > this_is_also_test_string12_6
    > EOF
    this_is_test_string1 22
    this_is_also_test_string12 6
    

提交回复
热议问题