Shell string processing, getting file names and suffix names
Source: Internet
Author: User
"http://www.lichaozheng.info/2012/03/20/shell-get filename and suffix name/code: file=" Thisfile.txt "echo" FileName: ${file%.*} "echo" Extension: ${file##*.} "Output: Filename:thisfile extension: txt: Bash string processing matching $OLD/$NEW} based on pattern ${str/substring Replace the first one. ${str//$OLD/$NEW} replaces all. Note: You cannot use regular expressions, you can only use the shell extensions of? *. can only use shell wildcard characters such as *? [list] [!list] [A-z]. ${str/# $OLD/$NEW} replaces the beginning. If Str starts with an old string, it is replaced. ${str/% $OLD/$NEW} replaces the end. If Str ends with an old string, it is replaced. [user@laptop ~]# str= ' Hello World ' [User@laptop ~]# Echo ${str/o/o} Hello World [User@laptop ~]# echo $ {str//o/o} Hello World [user@laptop ~]# str= ' Hello World ' [User@laptop ~]# echo ${str/#He/he} Hello worl d [User@laptop ~]# echo ${str/#o/he} Hello World [user@laptop ~]# Echo ${str/%he/he} Hello World [user@laptop ~]# Echo ${str/%ld/ld} Hello World If the string contains/characters are replaced, then escape, write \/. [User@laptop ~]# filename= "/user/admin/monitoring/process.sh" [User@laptop ~]# Echo ${filename/#\/user/\ /tmp} /TMP/ADMIn/monitoring/process.sh [User@laptop ~]# echo ${filename/%.*/.ksh} /user/admin/monitoring/process.ksh [ User@laptop ~]# Separate directories for the environment variable path, one for each row. ECHO-E ${path/:/\n} [User@laptop ctmw]# echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/apache/ Apache-ant-1.7.1/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/user/bin [User@laptop ctmw]# Echo-e ${path//:/' \ n '} /usr/kerberos/sbin/usr/kerberos/bin/usr/apache/apache-ant-1.7.1/bin/usr/local/sbin Usr/local/bin/sbin/bin/usr/sbin/usr/bin/user/bin [User@laptop ctmw]# echo ' ${path//:/$ ' \ n '} ' /usr/kerberos/ Sbin/usr/kerberos/bin/usr/apache/apache-ant-1.7.1/bin/usr/local/sbin/usr/local/bin/sbin/bin/usr/sbin/usr/bin/ User/bin SUBSTRING deletion based on pattern matching deletion is a special kind of substitution ${str/$SUB} deletes the first sub substring in str ${str//$SUB} Deletes all sub substrings in Str ${str#$ PREFIX} Go to the head, remove the shortest matching prefix from the beginning ${str## $PREFIX} go to the head, remove the longest matching prefix from the beginning ${str% $SUFFIX} go tail, remove the shortest matching suffix from the end ${str%% $SUFFIX} go tail, remove the longest matching suffix from the end Note: Often remember the meaning of # and%, there is a way to help memory look at the keyboard, #Before $,% after $, just know # go head,% go tail. Note: You cannot use regular expressions, you can only use the shell extensions of? *. [user@laptop ~]# str= ' Hello world ' [User@laptop ~]# Echo ${str#he} llo World [User@laptop ~]# Echo ${st r#he*o} World [User@laptop ~]# echo ${str# #He *o} rld [user@laptop ~]# prefix= "*o" [User@laptop ~]# Echo ${str# $PREFIX} World [User@laptop ~]# echo ${str## $PREFIX} rld [user@laptop ~]# echo ${str%o*} Hello W [ User@laptop ~]# echo ${str%%o*} Hell [user@laptop ~]# suffix= "o*" [User@laptop ~]# echo ${str% $SUFFIX} H Ello W [User@laptop ~]# echo ${str%% $SUFFIX} Hell Typical application: Get file extension [user@laptop ~]# file=hello.jpg [user@l Aptop ~]# echo ${file##*.} jpg using the SED command to implement regular expression substitution use the SED command to make regular expression substitutions. echo "$STR" | Sed "s/$OLD/$NEW/" replaces the old substring in str with NEW. [User@laptop ~]# str= 123456789″ [User@laptop ~]# echo "$STR" | Sed s/345/ok/ 12ok6789 [user@laptop ~]# old=345 [user@laptop ~]# new=ok [User@laptop ~]# echo "$STR" | Sed "s/$OLD/$NEW/" 12ok6789 using the TR command to implement the substitution of character sets using the TR command, you can replace a character, and it can be a replacement from a batch of characters to another. For example, lowercase letters become uppercase letters, or vice versa. [User@laptop ~]# echo "Bash" | TR "[A-z]" [b-z] CBTI The above command is to replace a in the original string with a B, replaced by C, and so on. Online question: Is there a command in Linux that can replace the + or-or-or + that is "+"-->-" "-"-->" + For example gmt+8-9 into gmt-8+9 [User@laptop ~]# echo "gmt+8-9″| Sed ' s/-/#/g ' | Sed ' s/+/-/g ' | Sed ' s/#/+/g ' gmt-8+9 above are the answers provided online. If you do it in TR, it's simpler. [User@laptop ~]# echo "gmt+8-9″| TR "+-" "-+" gmt-8+9 path string processing dirname ${fullpath} Fetch directory section. BaseName ${fullpath} takes the filename part. BaseName ${fullpath} ${ext} takes the filename part and removes the specified extension. [User@laptop ~]# fullpath=/user/work/project/backup.tar.gz [user@laptop ~]# dirname "$FULLPATH" /user/ Work/project [user@laptop ~]# basename "$FULLPATH" backup.tar.gz [user@laptop ~]# basename "$FULLPATH". GZ&N Bsp Backup.tar [user@laptop ~]# basename "$FULLPATH" .tar backup.tar.gz [user@laptop ~]# "$FULLPATH". basename Backup directory section: ${fullpath%/*} (similar to DirName "$FULLPATH") take file name: file=${fullpath##*/} (similar to basename "$FULLP ATH ") Take the shortest basic name: ${file%%.*} take the longest base name: ${file%.*} take the shortest extension: ${file##*.} or ${fullpath##*.} take the longest extension: ${file#*.} or ${fullpath#*.} [User@laptop ~]# fullpath=/user/work/project/backup.tar.gz [user@laptop ~] # echo ${fullpath%/*} /user/work/project [user@laptop ~]# dirname ' $FULLPATH ' /user/work/project [ User@laptop ~]# file=${fullpath##*/} [User@laptop ~]# echo $FILE backup.tar.gz [user@laptop ~]# basename] $FU Llpath " backup.tar.gz [user@laptop ~]# echo ${file%%.*} backup [User@laptop ~]# echo ${file%.*} backup. tar [User@laptop ~]# echo ${file##*.} GZ [User@laptop ~]# echo ${file#*.} tar.gz [User@laptop ~]#
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.