The practice of Shell programming is genuine (code example)

Source: Internet
Author: User

1, in any folder, create a new 10 empty files, the file name is T1.txt-t10.txt, the file content is 11-20, on a line, and then the 10 files named as t1.sh-t10.sh.

Old driver Version:

$ for ((i=1;i<=10;i++));d o p= ' expr $i + ten '; echo $p >> "T" ${i}.txt; Done #创建加写入

$ for fie in ' ls *txt ';d o p= ' basename $fie. txt '; MV $fie ${p}.sh;d one #重命名

Small rookie version:

#!/bin/bash

For i in {1..10} #创建加写入

Do
Touch Test$i.txt
Echo ' expr $i + ten ' >test$i.txt
Done

Rename ' s/txt/sh/' * #重命名

Problem:

The two commands, sed and rename, are quite esoteric and will be summed up sometime. Refer to: https://www.cnblogs.com/AloneSword/p/5072698.html

sed command:

There are haha.sh in the current folder, the contents are:

Abc

Cdfa

Ghia

Jkla LM=ABC

Mnoa

Pqr

Stu

Vwx

Yz

basic format:

sed [options] 'command ' file (s), for example: instance 1:sed ' s/a/b/g ' filename note, is a single quotation mark (most of the time a single-double generic, but a few cases of a single pair, such as \$). Default in the output is changed, Do not change in source file

Basic parameters:

-N Quiet mode, does not display the full text, only shows the changed or command-related lines, and p with use OH. P: Print the specified line

-I does not just output the results at the terminal, but also modifies the results to the source file: Sed-i ' s/a/a/g ' haha.sh

-R with extended regular expression matching, default regular regular expression,

Sed-r "s/(lm=) */\1 bcd/g" haha.sh equivalent to Sed-r "s/lm=.*/lm=bcd/g" haha.sh, but obviously a lot of high end. For example, the following:

      echo "Wo he ni" |sed-r ' s/(wo) (. *) (NI)/\3\2\1/g '    sed The order of the parentheses by default is \1,\2,\3, function returns ni he wo

-E supports SED with multiple commands

Sed-e "s/a/a/g;s/b/b/g" haha.sh or Sed-e "s/a/a/g"-E "s/b/b/g" haha.sh replace both A and B in the file, the first example can be no-e.

- F Specifies that the command in the filename file should be executed with the Compile function

Suppose there is a file named tmp, the contents of the file is s/a/a/g;s/b/b/g, then we can replace it with TMP directly.

SED-F tmp haha.sh Replace both A and B in the file

commands specific to sed: such as the ' s ' and ' G ' in the previous instance 1

s denotes the substitution of words or letters in the line: s is similar to the y-axis, G is similar to the x-axis, S-position line, and the first in the G-Position row

Sed ' s/a/a/g ' haha.sh the full text to replace a with a

Sed ' 1s/a/a/g ' haha.sh only replaces a in the first line with a, the default output haha.sh all of the replaced content, plus-n, is the output of the changed content, that is, the first line changed

Sed ' s/a/a/2g ' haha.sh the second A in every row into a,

The sed ' 1s/.*/aini/g ' haha.sh replaces the entire line of the first line with the aini,.* meaning that the regular matches all characters.

The sed "s/haha/${a}/g" haha.sh replaces the text haha with a variable A, which can be assigned by itself, or it can be calculated by other commands or programs.

C Full line substitution

Sed ' C 123 ' haha.sh The default is to replace all rows with 123, without tube content, (spaces can be replaced with slashes)

Sed ' 2c 123 ' haha.sh changed the second line to 123, and the other unchanged.

A equals add, adding a line to the specified line behind

Sed ' a 123 456 789 ' haha.sh By default adds 123 456 789 to each line

Sed ' 1a 123456 ' haha.sh add a row after the first line 123456

P Prints the specified line, usually in conjunction with the- n parameter

Sed-n ' s/a/a/p ' haha.sh prints out all the lines that have been replaced, note that the p here cannot be substituted with G.

Sed-n ' 5p ' haha.sh print out line fifth of haha.sh

Sed-n ' 2,5p ' haha.sh print out 2-5 lines of haha.sh

Sed-n '/c.f/p ' haha.sh prints out all matching rows that match the regular C.F, note that the first slash is required

d Delete the specified line

Sed ' 1,5d ' haha.sh delete the specified line in haha.sh

Sed '/^\s*$/d ' haha.sh Delete empty lines

Rename Command

Basic Format:

sed [options] 'command ' file

Options:

- v display successfully renamed file instance 1:rename-v ' s/suibian/abc/' * Suibian renamed to ABC, will be accompanied by a hint: Suibian renamed as ABC

- N is not really renamed, but used as a test, the command will change the fileto be changed to print out the instance: Rename-n ' s/abc/suibian/' * The ABC into Suibian, the result: rename (ABC, s Uibian)

- f indicates that the modification is enforced.

the Replace command in rename

Basic and sed, commonly used s and g, in addition, these two commands are best to use only single quotation marks, single quotes internally support escape.

Example:

Rename ' s/a/a/g ' * all filenames in the current folder are replaced by a with a full. * Denotes all files.

Rename ' s/a/a/' *.txt d in all txt files under current folder, A has become a, "G" can be saved.

Rename ' s/$/s/' * Add a s,rename ' s/\$/s/' at the end of all the files to the file at the end of the file name, and change the file to the end of S. However, if you use double quotation marks or no quotation marks, whether or not you add a transfer symbol, the result is to add s at the end of each file, so that rename or more single quotes is good.

Rename-n ' s/\.sh//' *.sh remove the suffix from all. Sh End files.

Rename-n ' s/[1]/number/' *.sh contains the number 1 in the file name, 1 of which are replaced by numbers

common regular Match formats

x? Match 0 or one x string

        x* matches 0 or more x strings, but matches the minimum possible number of times         x+ matches 1 or more x strings, but matches the minimum possible number of times         . * matches any character 0 or one time         . + Match any character 1 or more times         {m} matches exactly a specified string of M         {M,n} matches a specified string below M + N         [] matches the characters in []        [^] matches characters that do not match []         [0-9] match all numeric characters         [A-z] matches all lowercase letters         ^ characters that match the beginning of a character        $ matches characters ending with characters        \d matches a number of characters, like [0-9] Syntax        \d+ matches multiple numeric strings, as with [0-9]+ syntax         \w A string of English letters or numbers, as in [a-za-z0-9] syntax         \w A string of non-English letters or numbers, as in [^a-za-z0-9] syntax         \s spaces, as with [\n\t\r\f] syntax         A|b|c matches strings that match the A or B or C characters

The practice of Shell programming is genuine (code example)

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.