Linux Shell Script Problem

I have a dot separated string in Linux Shell,

$example=This.is.My.String

I want to

1. Add some line to the last point, for example, I want to add "Good.Long" to the last point, so I get:

This.is.My.Goood.Long.String

2. Get the part after the last point, so I get

String

3.Turn the dot to the underscore except for the last dot, so I get

This_is_My.String

If you have time, explain a little, I am still studying regex.

Thank you so much!

+5
source share
6 answers

I do not know what you mean by "Linux Shell", so I guess bash. This solution will also work in zshetc:

example=This.is.My.String
before_last_dot=${example%.*}
after_last_dot=${example##*.}
echo ${before_last_dot}.Goood.Long.${after_last_dot} 
This.is.My.Goood.Long.String

echo ${before_last_dot//./_}.${after_last_dot} 
This_is_My.String

before_last_dot after_last_dot % ##. //, , , , - .

sed ( ), bash . script, : -)

+10

  • :
    • : sed 's/\(.*\)\([.][^.]*$\)/\1.Goood.Long\2/'
    • : sed 's/.*\./&Goood.Long./' -
  • ?
  • sed 's/\([^.]*\)[.]\([^.]*[.]\)/\1_\2/g'

3, , , () .

, sed \(...\) "", "\1" .

  • , , ( ); , , , , .

  • , ; .

  • , ( ), ; . , . , , ceil (log 2 N), N - , . 1 ; 2 3; 4-7 ..

+3

# 1 # 2. # 3. . , , . :

sed 's/\(.*\)\./\1\n./;h;s/[^\n]*\n//;x;s/\n.*//;s/\./_/g;G;s/\n//'
  • , :

    s/\(.*\)\./\1\n./;h
    
  • :

    s/[^\n]*\n//;x
    
  • ,

    s/\n.*//
    
  • s/\./_/g;G
    
  • ,

    s/\n//
    

sed script .

( ):

                                  Hold Space

  •         This.is.My \n.String       This.is.My \n.String

  •         This.is.My \n.String        .String

  •         This.is.My                        .String

  •         This_is_My \n.String      .String

  •         This_is_My.String            .String

+3

, Bash regex (Bash 3.2 ).

[[ $example =~ ^(.*)\.(.*)$ ]]
echo ${BASH_REMATCH[1]//./_}.${BASH_REMATCH[2]}

Bash, IFS ( ).

saveIFS=$IFS
IFS=.
array=($e)                    # *   split the string at each dot
lastword=${array[@]: -1}
unset "array[${#array}-1]"    # *
IFS=_
echo "${array[*]}.$lastword"  #     The asterisk as a subscript when inside quotes causes IFS (an underscore in this case) to be inserted between each element of the array
IFS=$saveIFS

* declare -p array , , .

+3

1.

$ echo 'This.is.my.string' | sed 's}[^\.][^\.]*$}Good Long.&}'
This.is.my.Good Long.string

: , . : ,

2.

$ echo 'This.is.my.string' | sed 's}.*\.}}'
string

sed , (. *) .

3.

$ echo 'This.is.my.string' | tr . _ | sed 's/_\([^_]*\)$/\.\1/'
This_is_my.string

_, _ .

(caveat: 'This.is.my.string_foo' 'This_is_my_string.foo', 'This_is_my.string_foo')

+2

( !), Awk .

1. echo $example| awk -v ins="Good.long" -F . '{OFS="."; $NF = ins"."$NF;print}'

:
-v ins = "Good.long" awk "ins" "Good.long" ,
-F. awk ,
-OFS awk ,
NF - , $NF ,
$ NF =... , , ( "ins" ​​).

2. echo $example| awk -F . '{print $NF}'

$NF - , !

3. echo $example| awk -F . '{OFS="_"; $(NF-1) = $(NF-1)"."$NF; NF=NF-1; print}'

, Awk AFAIK . , .

$(NF-1) = $(NF-1) "." $NF: -, , , .
awk, , , !

, $NF = "", .

+1

All Articles