shell实用实例技巧

本文介绍多种方法用于在Linux环境下获取网卡IP地址,并展示如何使用awk命令实现文本中数字的累加及合并奇偶行的多种方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.使用ifconfig取出网卡eth0的ip地址

方法1:
[root@puyang~]# ifconfig eth0 |sed -n '2p' |sed 's#^.*addr:##g'|sed 's#  B.*$##g'
192.168.142.133
方法2:cut
[root@puyang ~]# ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " " -f1
192.168.142.133
方法3:普通awk 使用2遍
[root@puyang ~]# ifconfig eth0|grep 'inet addr'|awk -F ":" '{print $2}'|awk '{print $1}'
192.168.142.133
方法4:awk同时多分隔符法
[root@puyang ~]# ifconfig eth0|grep 'inet addr'|awk -F '[ :]' '{print $13}'
192.168.142.133
方法5:awk同时多分隔符法
[root@puyang ~]# ifconfig eth0|sed -n '2p'|awk -F '[ :]+' '{print $4}'
192.168.142.133
[root@puyang ~]# ifconfig eth0 |awk -F '[ :]+' 'NR==2 {print $4}' 
192.168.142.133
方法6:sed(正则)
[root@puyang ~]# ifconfig eth0 |sed -nr '2s#^.*dr:(.*)  B.*$#\1#gp'           
192.168.142.133
方法7:grep-perl正则方法
[root@puyang ~]# ifconfig eth0|grep -Po '(?<=dr:)[0-9.]+'

192.168.142.133

2.awk累计相加

假如现在有个文本,格式如下:
a  1
b  3
c  2
d  7
b  5
a  3 
g  2
f  6
d  9
即左边是随机字母,右边是随机数字,要求写个脚本使其输出格式为:
a  4
b  8
c  2
d  16
f  6
g  2
即将相同的字母后面的数字加在一起,按字母的顺序输出。
答案:
awk '{h[$1]=h[$1]+$2}END{for(pol in h)print pol,h[pol]}' array_add.txt 
演示:
[root@show awkfile]# awk '{h[$1]=h[$1]+$2}END{for(pol in h)print pol,h[pol]}' array_add.txt 
a 4
b 8
c 2
d 16
f 6
g 2
过程详解:
第一个里程碑-把第二列的数字的总和
[root@show awkfile]# cat array_add.txt 
a  1
b  3
c  2
d  7
b  5
a  3
g  2
f  6
d  9
[root@show awkfile]# awk '{sum=sum+$2}END{print sum}' array_add.txt 
38
第二个里程碑-选择什么作为房间号码
这里只有两列,第一列是字母,第二列是数字是我们要相加的东西。这里我们选择第一列作为房间号码即元素名称。
第三个里程碑-遇到相同的字母再相加
这里就需要用到了awk数组的内容。
sum=sum+$2 这里把sum换成awk数组即可。
[root@puyang~]# awk '{h[$1]=h[$1]+$2}' array_add.txt
没有显示任何过程,不是很好。我们看看他到底如何处理的。一步一步来分析。注意这里我们只关注一个房间a,大家熟练后再同时看多个房间的内容。
[root@puyang~]# awk '{h[$1]=h[$1]+$2;print "当前行内容:"$0,"a房间内容:"h["a"]}' array_add.txt 
当前行内容:a  1 a房间内容:1
当前行内容:b  3 a房间内容:1
当前行内容:c  2 a房间内容:1
当前行内容:d  7 a房间内容:1
当前行内容:b  5 a房间内容:1
当前行内容:a  3 a房间内容:4
当前行内容:g  2 a房间内容:4
当前行内容:f  6 a房间内容:4
当前行内容:d  9 a房间内容:4
我们只关注a房间的内容及h["a"]的内容
行号 当前行的内容 $1的内容 $2的内容 h["a"]之前的内容 h["a"]=h["a"]+$2h["a"]之后的内容
1 a  1 a 1 h["a"]=空+11
2 b  3 b 3 1不是a房间,不进行计算,h["a"]内容还是11
3 c  2 c 2 1不是a房间,不进行计算,h["a"]内容还是11
4 d  7 d 7 1不是a房间,不进行计算,h["a"]内容还是11
5 b  5 b 5 1不是a房间,不进行计算,h["a"]内容还是11
6 a  3 a 3 1h["a"]=1+34
7 g  2 g 2 4不是a房间,不进行计算,h["a"]内容还是14
8 f  6 f 6 4不是a房间,不进行计算,h["a"]内容还是14
9 d  9 d 9 4不是a房间,不进行计算,h["a"]内容还是14
似曾相识的表格呀,我看结合这个表格看看执行过程。
首先读取第一行:
$1内容是a,$2内容是1,这里我们也创建了数组h[$1],我们要计算h[$1]=h[$1]+$2,即h["a"]=空+1.此时h["a"]的内容是1.
然后读取第二行:
$1内容是b,不是我们要的a房间我们跳过,此时h["a"]的内容依然是1
继续往下读情况都是一样的直到读取到第六行
$1内容是a,$2内容是3,这里我们也创建了数组h[$1],我们要计算h[$1]=h[$1]+$2,即h["a"]=1+4.此时h["a"]的内容是4.
awk继续往下读取直到读取到最后一行$1都不是a,所以h["a"]房间的内容不会发生变化。
现在大家再看一下执行过程,注意我们只关注h["a"]房间的内容。
[root@spuyang~]# awk '{h[$1]=h[$1]+$2;print "当前行内容:"$0,"a房间内容:"h["a"]}' array_add.txt 
当前行内容:a  1 a房间内容:1
当前行内容:b  3 a房间内容:1
当前行内容:c  2 a房间内容:1
当前行内容:d  7 a房间内容:1
当前行内容:b  5 a房间内容:1
当前行内容:a  3 a房间内容:4
当前行内容:g  2 a房间内容:4
当前行内容:f  6 a房间内容:4
当前行内容:d  9 a房间内容:4
这时候你应该理解他到底如何执行的了。

3.将一个文本的奇数行和偶数行合并

方法一:sed版本
[root@puyang ~]# sed 'N;s#\n# #g' 0324-join.txt

方法二:xargs初级版
[root@puyang ~]# xargs -n14 <0324-join.txt 

方法三:xargs升级版
[root@puyang ~]# xargs -d "\n" -n2 <0324-join.txt 

方法三:awk方法
[root@puyang ~]# awk  '{lastline=$0;getline;print lastline,$0}' 0324-join.txt

方法四:awk-getline升级版
[root@puyang ~]# awk '{getline lastline;print lastline,$0}' 0324-join.txt 

方法五:paste-sd参数
[root@puyang ~]# paste -sd " \n" 0324-join.txt 

方法六:
[root@puyang ~]# paste -d " " - - <0324-join.txt 

方法七:判断奇偶行并显示
[root@puyang ~]# awk '{if(NR%2==0) print $0; else printf $0" "}' 
方法八:判断奇偶行显示-升级版
[root@puyang ~]# awk '{printf NR%2?$0:$0"\n"}'  0324-join.txt 

方法九:
[root@puyang ~]# awk 'NR%2==1{line=$0;next}{print line,$0}' 0324-join.txt 

方法十:
[root@puyang ~]# awk 'ORS=NR%2==0?"\n":" "' 0324-join.txt 


Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值