Shell modified file name (a)

Source: Internet
Author: User

If the file name is: Time_filename.txt changed to Filename_time.txt. For example 20111111_me.txt change to me_201111111.txt how to modify?

1#! /bin/SH2  forEachfileinch`ls-B '3  Do4Filename=${eachfile%. txt}5Filehead= 'Echo$filename |awk-F _'{print $}'`6Filelast= 'Echo$filename |awk-F _'{print $}'`7  MV$filename. txt ${filelast}_$filehead.txt8  Done

Description

The default file you want to work with is in a folder with the suffix txt:
The 2nd line is to list all the files, and then 4-7 lines of processing for each file;
The 4th line is to get the file name, not including the suffix txt;
Then the filename is divided into two parts: Filehead and Filelast;
Finally, rename the source file to Filelast_filehead.txt.

Add:

${},# #和 percent usage in the shell

Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt

Different values can be replaced with ${}, respectively:

1. Truncation function
${file#*/}: Remove the first one/And the string to its left:Dir1/dir2/dir3/my.file.txt
${file##*/}: Erase the last one/And the string to its left:My.file.txt
${file#*.}: Remove the first one.And the string to its left:File.txt
${file##*.}: Erase the last one.And the string to its left:Txt
${file%/*}: Erase the last one//dir1/dir2/dir3
${file%%/*}: Delete the first null value Span style= "color: #333333;" >)
${file%.*}: Erase the last one.  .   and its right string: /dir1/dir2/dir3/my.file
${file%%.*}: Delete the first  .   and its right string: /dir1/dir2/dir3/my

Memory by:
#是去掉左边 (on keyboard # on the left of $), # #最后一个;
% is remove right (on keyboard% in $ to the right), and the first one.
/span>

< Span style= "color: #333333;" >< Span style= "color: #333333;" >< Span style= "color: #333333;" >2. String Extraction

< Span style= "color: #333333;" > single A symbol is the minimum match; two symbols are the maximum match
${file:0:5}: Extract the leftmost 5 bytes: /dir1
${file:5:5} : Extract the 55 bytes: /dir2

3. String substitution

You can also replace the string in the value of the variable:
: The first dir Replace with path:/path1/dir2/dir3/my.file.txt
${file//dir/ Path}dir Path:/path1/path2/path3/my.file.txt
/span>

< Span style= "color: #333333; Background:white; " > 4. < Span style= "color: #333333;" > assign values for different variable states (no setting, null value, non-null value):
${file-my.file.txt}: If the $file is not set, use My.file.txt as the return value. (null and non-null values are not processed)
${file:-my.file.txt}: If $file is not set or null, use My.file.txt as the return value. (Non-null value is not processed)
${file+my.file.txt}: Use My.file.txt as the return value if $file is set to a null or non-null value. (not processed when not set)
${file:+my.file.txt}: If $file is a non-null value, use My.file.txt as the return value. (No processing when not set and null value)
${file=my.file.txt}: If $file is not set, My.file.txt is used as the return value, and $file is assigned my.file.txt. (null and non-null values are not processed)
${file:=my.file.txt}: If $file is not set or null, use My.file.txt as the return value and assign the $file to My.file.txt. (Non-null value is not processed)
${file?my.file.txt}: If $file is not set, output my.file.txt to STDERR. (null and non-null values are not processed)
${file:?my.file.txt}: If $file is not set or null, the my.file.txt output to stderr. (Non-null value is not processed)

${#var}
${# File} can get 27 because /dir1/dir2/dir3/my.file.txt 27 bytes.

Note:
The ": +" case does not contain a null value.
":-", ": =" as long as there is a number is a null value (NULL).


5. Length of the variable
${#file}


6. Array operations
A= (a b c def)
${a[@]} or ${a[*]} to get A b C def (total number of groups)
${a[0]} can get a (number of first group), ${a[1]} is the second group number ...
${#A [@]} or ${#A [*]} to get 4 (total number of groups)
${#A [0]} can get 1 (that is, the length of the first group (a), ${#A [3]} can get 3 (fourth group number (def) length)

Http://www.2cto.com/os/201310/248691.html

Http://space.baidu.com.cn/ugo5/blog/item/c550bbc54d1644079c163dbd.html

Shell modified file name (a)

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.