Some commonly used shell recorded.
1) query the total size of each subdirectory under the current directory in m
ll|awk '{print $9}'|xargs -i du -m -s {}
2) commonly used sed command
1.1 Printing from the second line to the last line. ll | sed -n '2, $ p'
explain:
Because ll will be above the line is a total footprint, in some cases you do not need this line
-n is needed to be added, and without it will print once, qualified and print once, is the first line of play once, twice hit the other line
1.2 sed inside the regular expression to escape attention
p_path='aa/bb/cc'
sub_path ='/bb/cc'
reg_sub_path = `echo $ {sub_path} | sed 's / \ // \\\\\ // g' # the backslash \ / Switch \\\\\ / reg_sub_path case of \\ / bb \\ / cc
sed 's /' $ {reg_sub_path} '// g' # function is removed the phrase \ bb \ cc left \ AA, here corresponding to sed 's / \ / bb \ / cc // g' as $ {reg_sub_path} variable inside sed into a recognizable character to a regular time \\ conducted an escape into a \ and / did not escape, sed regular but also by \ / to match /
1.3 Regular determination logic and other considerations of sed
1) regular braces inside to escape, no escape in parentheses
echo aabbccddeeff | sed 's / [ ^ f] \ {0, \} // g' output FF
2) in parentheses indicates the presence or absence ^, and ^ represents all the brackets only at one of the first value does not appear.
echo aabbccddeeff | sed 's / [ ^ f | b] \ {0,100 \} // g' output bbff
. 3) or needs to be escaped character \ | comma need to escape
echo aabbccddeeff | sed 's / [ bcde] \ { 1,1 \} \ | [a] \ {1,1 \} // g ' output FF
. 4) do not exist and regular determination, only two results or can not be found, to find the meaning or twice
5) regular play ^, end $
6) to judgment is borrowed sed functions accomplished as echo aabbccddeeff | sed '/ [acb ] \ {0, \} / b fun: fun / [cb] \ {0 , \} / b fun2: fun2 s / bc // g ' output aabcddeeff
there are three conditions:
a first condition is satisfied [acb] \ {0, \ } to find the second condition [cb] \ {0, \ } the second condition is satisfied is to find a third operation, a replace command is s / bc // g
However, to note: Although this alternative is the result of three conditions, but the first two are looking for this entire line meets this regular, is the condition of the third row to match the string in the row.
2) commonly used awk command
2.1 awk print command at the console is the basic format ll | awk '{print $ 9}'
explain:
After ll out of the ninth column is the file name of the child, this is the most commonly taken
print $ 9 ninth column is printed, and each line is printed once
printl $ 9 is a summary of all the lines once again print this distinction is important
2.2 awk -v parameter
export -f fun herein is a method of fun shell system into a command recognized, so that after the system will define the method of fun parsed into shell code into a shell script to call
ll | awk -v prm1=${imput} -v prm2=$1 '{ cmd="fun prm1 prm2 " system(cmd) }'
explain
It is defined in a -v '{}' variables which can be used
export -f
3) common xargs -i command
3.1 ll | awk '{ print $9}' |xargs -i echo {}
xargs is to enter each line as a
-i is a placeholder for the {} is the placeholder.
Other
Row variable column, the column becomes row, all rows have the same number of columns, all columns have the same number (square) line
ll | awk '{print $ 4, $ 9}' | sed -n '2,10p' # transposed before
root app
root bin
root boot
root data
root dev
root dist
root etc
root git.repositories
root home
ll|awk '{print $4,$9}'|sed -n '2,10p' |awk -F " " '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=NF;i++) {{for(j=1;j<=NR;j++) {printf a[i,j] " "}}printf "\n"}}' |column -t
After transpose
root root root root root root root root root
app bin boot data dev dist etc git.repositories home
Applicable to the field must be fixed in order to specify a symbol separator chaos is assumed here that the source data separated by a comma
root , app , root
root , bin , root
root , , root
root , data , root
root , dev , root
root , dist , root
,etc , root
root , git.repositories , root
root , home ,
cat info | tr -s [:space:] |awk -F "," '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=3;i++) {{for(j=1;j<=NR;j++) {printf a[i,j]; if(j!=NR){printf","}}}printf "\n"}}'|column -s ',' -t -o ','
Row after row turn become
root ,root ,root ,root ,root ,root , ,root ,root
app , bin , , data , dev , dist ,etc , git.repositories , home
root, root, root, root , root, root , root, root ,
Can use
Many would like to turn into a row of data such as line
Monday, app, root
on Tuesday, bin,
Wednesday,
Thursday, data, root
on Friday,
cat info |tr "\n" "."|sed -e 's/.$/\n/'
Then, it becomes the following line, the line number converter is a full stop, and then remove the last line period
Monday, app, root. Tuesday, bin,. Wednesday, Thursday, data, root. On Friday,
Line represents what good is it? It can be used as a parameter to the function processing
Then | "." Cut -d -f 2
We get the second row of the original data
Tuesday, bin,
Then | cut -d "," -f 1
The first column is obtained source data
Becomes a single row
This is a one-way
ll|sed -n '2,2p'
One-way
Ranks Huzhuan
| awk -F " " '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=NF;i++) {{for(j=1;j<=NR;j++) {printf a[i,j] " "}}printf "\n"}}'
Of course, you can also turn back along with the phrase,
Also can separate switch
tr "\n" " "|sed -e 's/" "$/\n/'