Written Exercises

First, for the / etc / passwd operation

1, sed operation, the ninth to fifteenth rows copied file to the next line, line 16.

[root@ns1 lianxi]# sed '9,15H;16G' /etc/passwd

2, last column of the second field of the file acquired in the third row with awk.

[root@ns1 lianxi]# awk -F: 'NR==3{print $(NF-1)}' /etc/passwd

Second, for the test.txt file operations

1, the first column of each row of the summed row aaa

[root@ns1 lianxi]# sed -n '/^aaa/p' test.txt | awk '{print $2+$3+$4+$5}'

[root@ns1 lianxi]# sed -n '/^aaa/p' test.txt | awk '{sum=0;for(i=2;i<=NF;i++){sum+=$i}print sum}'

[root@ns1 lianxi]# awk '(NR%2)==1{{sum+=$2+$3+$4+$5}print sum}' test.txt 

2, the even-numbered row summation of the file

[root@ns1 lianxi]# awk '(NR%2)==0{print}' test.txt | awk '{sum=0;for(i=2;i<=NF;i++){sum+=$i}print sum}'

[root@ns1 lianxi]# awk '(NR%2)==0{print}' test.txt | awk '{print $2+$3+$4+$5}'

[root@ns1 lianxi]# awk '(NR%2)==0{{sum+=$2+$3+$4+$5}print sum}' test.txt

The maximum value of 3, extracted file

[root@ns1 lianxi]# awk '{print $2,$3,$4,$5}' test.txt | sed 's/ /\n/gp' | sort -nr | head -1

4, all the spaces in the file test.txt replaced!

[root@ns1 lianxi]# sed 's/ /!/gp' test.txt

Three, case, which, continue usage

1, case usage

shell script case statement may be combined read instructions selected to achieve good interaction response operation, the read command received incoming case one or more parameters, and selecting operation according to the parameters do case.

in case the value of the variable
mode 1)
command sequence
;;
Mode 2)
command sequence 2
;;
*)
default command sequence
esac

2, which usage

Find command is a command which exists and which is stored in the position command.

3, continue usage

continue the current cycle is not executed, and skip to the next cycle.

Fourth, the use of concurrent requests ssh command to view its TCP link status 

[root@ns1 lianxi]# netstat -anpt | grep :22 | awk '{print $6}' | sort  | uniq -c

Fifth, meaning ps aux the representatives of VSZ and RSS

a display of all associated with the terminal process, initiated by the terminal.
the X-display all associated with a terminal process.
U to display user-oriented user list.
VSZ virtual memory set, the process takes virtual memory space
RSS physical memory set, the process of war with the actual physical memory space.
S interruptable sleep state
R operating mode
D uninterruptible sleep state
T stop state dead state Z

Six shell script to write the following: for loop random lowercase letters plus the fixed length of 10 test string 10 to create a batch file in html / opt, uppercase and html.

for i in {1..10}
do
aa=$(tr -dc 'a-z' </dev/urandom | head -c 10)
touch /opt/$aa'_test.HTML'
done

Seven, ten randomly generated numbers, letters ten randomly generated, randomly generated 10 letters + numbers of mixed, hybrid digital special symbol + + ten letters randomly generated.

[root@ns1 lianxi]# tr -dc '1-9' </dev/urandom | head -c 10;echo

[root@ns1 lianxi]# tr -dc 'a-z' </dev/urandom | head -c 10;echo

[root@ns1 lianxi]# tr -dc 'a-z1-9' </dev/urandom | head -c 10;echo

[root@ns1 lianxi]# tr -dc 'a-z1-9$@!#%&' </dev/urandom | head -c 10;echo

 

Guess you like

Origin www.cnblogs.com/tanxiaojuncom/p/11494061.html