热度 4|
Linux Shell 练习手册
1、 在文件的行首插入指定字符。(sed)
[root@localhost ~]# seq 5 | sed 's/^/@/' @1 @2 @3 @4 @5 插入单引号 [root@localhost ~]# seq 5 | sed "s/^/\'/" '1 '2 '3 '4 '5
2、 显示系统根目录的磁盘利用率,不显示百分号。(awk,sed)
[root@localhost ~]# df -h | grep "/$" | awk ' {print $5}' | sed 's/%//' 36 [root@localhost ~]# df -h | grep "/$" | awk ' {print +$5}' 36
3、 从某文档中一次读取一行,并赋值给变量。(while,for)
[root@localhost ~]# head /etc/passwd > test.txt [root@localhost ~]# while read line > do > list=$line > done < test.txt [root@localhost ~]# for line in $(cat test.txt) > do > echo $line > done
4、 筛选IP地址
[root@localhost script]# ifconfig | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' 192.168.0.210 192.168.0.255 255.255.255.0 127.0.0.1 255.0.0.0 [root@localhost script]# ifconfig | grep -oP '(\d+\.){3}\d+' 192.168.0.210 192.168.0.255 255.255.255.0 127.0.0.1 255.0.0.0 #使用Perl正则
5、 grep crond与grep [c]rond的区别
[root@localhost script]# ps aux | grep crond root 1056 0.0 0.0 117212 1280 ? Ss Feb10 0:21 crond root 10776 0.0 0.0 103240 856 pts/0 S+ 18:42 0:00 grep crond [root@localhost script]# ps aux | grep [c]rond root 1056 0.0 0.0 117212 1280 ? Ss Feb10 0:21 crond #[c]rond可以匹配crond,但不会匹配自身。
6、 awk多分隔符的使用
[root@localhost script]# cat time.txt 09:00:01,more aaa bbb ccc 09:00:02,more aaa bbb ccc 09:00:03,more aaa bbb ccc 09:00:04,more aaa bbb ccc 09:00:05,more aaa bbb ccc [root@localhost script]# awk -F ",| " '{print $1,$3,$4,$5}' time.txt 09:00:01 aaa bbb ccc 09:00:02 aaa bbb ccc 09:00:03 aaa bbb ccc 09:00:04 aaa bbb ccc 09:00:05 aaa bbb ccc
7、 查找一个网段内存活的主机。
[root@localhost script]# for ip in 192.168.0.{1..255} > do > ping $ip -c2 &>/dev/null > if [ $? -eq 0 ];then > echo $ip is alive > fi > done 192.168.0.1 is alive 192.168.0.2 is alive 192.168.0.5 is alive 192.168.0.6 is alive 192.168.0.7 is alive #这种方法效率低 [root@localhost script]# fping -a -g 192.168.0.1/24 2>/dev/null 192.168.0.1 192.168.0.2 192.168.0.5 192.168.0.6 192.168.0.10 192.168.0.17 #fping命令效率比较高,-a是检查存活的系统,-g 192.168.0.1/24 产生扫描的目标列表 。 [root@localhost script]# nmap -v -sP 192.168.0.1/24 | grep -B1 "up" #nmap也比较快
8、 一道腾讯的面试题
1 2 4 8
5 8 0 2
3 1 8 3
7 0 2 7
3 9 1 0
3 2 7 4
假如把第2列和第3列的值作为新的第5列,第5列的平均值为avg5,求第5列中大于avg5的行数。
[root@localhost script]# awk '{x+=$2+$3;a[NR]=$2+$3} END{y=x/NR;for(i in a){if(y<a[i]) z++} print z}' number.txt 4 下例是一个更详解的awk脚本实现 #!/bin/awk -f BEGIN {} { x+=$2+$3 a[NR]=$2+$3 print $0,a[NR] } END { y=x/NR printf "The sum: %d\n", x printf "The average: %d\n",y for ( i in a ) { if (y<a[i]) z++ } printf "The counter: %d\n",z } [root@localhost script]# ./tt.sh number.txt 1 2 4 8 6 5 8 0 2 8 3 1 8 3 9 7 0 2 7 2 3 9 1 0 10 3 2 7 4 9 The sum: 44 The average: 7 The counter: 4
9、 将一个文件中的相邻两行相加
[root@localhost script]# cat n1.txt 1 2 3 4 5 [root@localhost script]# ./ta.sh n1.txt 1 + 2 = 3 2 + 3 = 5 3 + 4 = 7 4 + 5 = 9 [root@localhost script]# cat ta.sh #!/bin/awk -f { a[NR]=$1 } END{ for (i=2;i<=NR;i++) { printf "%d + %d = %d \n",a[i-1],a[i],a[i]+a[i-1] } }
10、 for循环的几种写法
[root@localhost script]# for i in `seq 3`;do echo $i;done 1 2 3 [root@localhost script]# for i in {1..3};do echo $i;done 1 2 3 [root@localhost script]# for ((i=1;i<=3;i++));do echo $i;done 1 2 3
11、 日志文件处理(awk)
[root@localhost script]# awk '{a[$1]+=$3} END {for ( i in a) print i,a[i]}' conn.txt 20120102 5202 20120103 1040 20120105 3600 20120107 1200 20120101 2020 [root@localhost script]# cat conn.txt 20120101 MAXConnection: 1000 20120102 MAXConnection: 1002 20120105 MAXConnection: 1600 20120101 MAXConnection: 1020 20120103 MAXConnection: 1040 20120102 MAXConnection: 1200 20120105 MAXConnection: 2000 20120107 MAXConnection: 1200 20120102 MAXConnection: 3000
12、 Bash中sed引入变量
[root@localhost ~]# HOST=1.1.1.1 [root@localhost ~]# echo "127.0.0.1"|sed s/127.0.0.1/"$HOST"/ 1.1.1.1 [root@localhost ~]# cat -n /etc/passwd | sed -n "$var1,$var2 p" 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin [root@localhost ~]# echo $var1 $var2 1 3
13、 查看文件中指定的行
[root@localhost ~]# cat -n /var/log/messages | sed -n '40,42p' 40 May 12 06:45:33 localhost nagios: 41 May 12 07:45:33 localhost nagios: 42 May 12 08:45:33 localhost nagios: [root@localhost ~]# cat -n /var/log/messages | awk 'NR==40,NR==42' [root@localhost ~]# cat -n /var/log/messages | head -42 | tail -3
14、 保留文件中指定类型的字符
[root@localhost ~]# echo "aaaa123bbbb456,.?ccc789" | sed 's/[^[:digit:]]*//g' 123456789
15、 将文件中单词的首字母大写
[root@localhost ~]# echo "tom and jerry" | sed -r 's/\b([a-zA-Z])/\u\1\u/g' Tom And Jerry
16、 对文件中的行、单词和字符进行迭代
#文本内容如下 [root@localhost script]# cat python.txt Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. #对行进行迭代 [root@localhost script]# cat line.sh #!/bin/bash #Filename:Line.sh while read line do echo $line done < $1 [root@localhost script]# ./line.sh python.txt #对单词进行迭代,文件中包含标点符号,可以用sed替换掉 [root@localhost script]# sed 's/[[:punct:]]//g' python.txt > python_nopun.txt #如果要保留标点,则需要将单词和标点使用空格分隔开 [root@localhost script]# sed s/[[:punct:]]/" & "/g python.txt # [root@localhost script]# cat word.sh #!/bin/bash #Filename:word.sh while read line do for word in $line do echo $word done done <$1 [root@localhost script]# ./word.sh python_nopun.txt Python is powerful and fast ……
#对字符进行迭代 [root@localhost script]# cat letter.sh #!/bin/bash #Filename:letter.sh while read line do for word in $line do for ((i=0;i<${#word};i++)) do echo ${word:i:1} done done done <$1 [root@localhost script]# ./letter.sh python_word.txt P y t h o n i s #从字符串中提取字符 ${string:start_position:no_of_characters} [root@localhost script]# str="abcde" [root@localhost script]# echo ${str:0:1} a [root@localhost script]# echo ${str:1:1} b
Archiver|手机版|感谢所有关心和支持过LinuxTone的朋友们 转载本站内容请注明原作者名及出处 ( 京ICP备08103151 ) |
GMT+8, 2021-2-25 16:33 , Processed in 0.011157 second(s), 10 queries , Apc On.
Powered by Discuz! X2 Licensed
© 2001-2011 Comsenz Inc.