Monday, December 5, 2011

Replace the end of a string

ubuntu1004@ubuntu1004:~$ cat -n replace_end.sh
     1 #!/bin/bash
     2 echo -n "Type a word:"
     3 read text1
     4 let len1=${#text1}
     5
     6 echo -n "Type the end to substitute:"
     7 read text2
     8
     9 let len2=${#text2}
    10
    11 difference=$len1-$len2
    12
    13 if [ $len2 -gt $len1 ]
    14 then
    15 echo "This is not possible!!!"
    16 exit
    17 else
    18 echo "${text1:0:$difference}$text2"
    19 fi
ubuntu1004@ubuntu1004:~$ ./replace_end.sh
Type a word:ubuntu1004
Type the end to substitute:0804
ubuntu0804

No comments:

Post a Comment