sed to delete the last few lines and delete the specified line
Reproduced original link:
http://blog.51cto.com/lspgyy/1305489
sed want to delete files in the specified line, is used to specify the line number can also be used to match the RE.
Delete the specified line [can specify the line number to delete, delete the matching string]
[root@Jason64-17 ~]# cat -n seq.txt 1 ok i will help you 2 understand sed usage 3 how to use it 4 and we should use it in view 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed 3d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed /should/d 1 ok i will help you 2 understand sed usage 3 how to use it 5 now let us 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed /ow/d 1 ok i will help you 2 understand sed usage 4 and we should use it in view 6 go 7 hello my name is [root@Jason64-17 ~]# cat -n seq.txt | sed -r /how\|should/d 1 ok i will help you 2 understand sed usage 5 now let us 6 go 7 hello my name is
Delete the last few lines
[root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# for((i=1;i<4;i++)); do sed -i '$d' seq01.txt ; done #C式for循环 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# i=1; while [ $i -le 3 ]; do sed -i '$d' seq01.txt; ((i++)); done #while循环 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# for i in `seq 3`; do sed -i '$d' seq01.txt ; done #bash for循环 [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 #until 循环 [root@Jason64-17 ~]# seq 5 > seq01.txt [root@Jason64-17 ~]# cat seq01.txt 1 2 3 4 5 [root@Jason64-17 ~]# i=3; until [ $i -le 0 ]; do sed -i '$d' seq01.txt ; ((i--)); done [root@Jason64-17 ~]# cat seq01.txt 1 2 [root@Jason64-17 ~]#
awk exercises
. 4 Wang
CUI. 3
Zhao. 4
Liu. 3
Liu. 3
Chang. 5
Li 2
CUI. 3
Zhao. 4
Liu. 3
Liu. 3
Chang. 5
Li 2
1 identify the character length of the first field 4 by
2 when the second column value is greater than 3, create a blank file named line current domain $ 1 (Touch $ 1)
3 in the document string with liu Hong
. 4 of the second column and seeking
the average of the second column is seeking 5
6 selecting the maximum value in the second column
after the first column of the filter 7 is repeated, each of the lists, the number of occurrences of each item, each the sum of the sizes
2 when the second column value is greater than 3, create a blank file named line current domain $ 1 (Touch $ 1)
3 in the document string with liu Hong
. 4 of the second column and seeking
the average of the second column is seeking 5
6 selecting the maximum value in the second column
after the first column of the filter 7 is repeated, each of the lists, the number of occurrences of each item, each the sum of the sizes
1, a string
awk 'length ($ 1) == ". 4" {Print $ 1}'
2, execution system
awk '{IF ($ 2>. 3) {System ( "Touch" $ 1)}}'
. 3, gsub ( / r /, "s", domain) in the specified domain (default $ 0) with s an alternative R & lt (Sed 's /// G')
awk '{gsub (/ Liu /, "Hong", $. 1); Print $ 0 } 'a.txt
. 4, column summation
DF -H | awk' $ {2} = A + Print the END {A} '
. 5, the column averaging
df -h | awk' {a + = $ 2} END {print a / NR } '
DF -H | awk' {A + = $ 2; B ++} the END {Print A, A / B} '
. 6, column selecting the maximum value
df -h | awk' BEGIN {a = 0} {if ($ 2> a) the END = $ 2} a {a} Print '
. 7, each of the filters listed in the first column is repeated the number of times each occurrence, each of the total size
awk' {a [$ 1] ++; b [$ 1 ] + = $ 2} END { for (i in a) {print i, a [i], b [i]}} '
awk 'length ($ 1) == ". 4" {Print $ 1}'
2, execution system
awk '{IF ($ 2>. 3) {System ( "Touch" $ 1)}}'
. 3, gsub ( / r /, "s", domain) in the specified domain (default $ 0) with s an alternative R & lt (Sed 's /// G')
awk '{gsub (/ Liu /, "Hong", $. 1); Print $ 0 } 'a.txt
. 4, column summation
DF -H | awk' $ {2} = A + Print the END {A} '
. 5, the column averaging
df -h | awk' {a + = $ 2} END {print a / NR } '
DF -H | awk' {A + = $ 2; B ++} the END {Print A, A / B} '
. 6, column selecting the maximum value
df -h | awk' BEGIN {a = 0} {if ($ 2> a) the END = $ 2} a {a} Print '
. 7, each of the filters listed in the first column is repeated the number of times each occurrence, each of the total size
awk' {a [$ 1] ++; b [$ 1 ] + = $ 2} END { for (i in a) {print i, a [i], b [i]}} '