linux基本操作---grep、sed、awk

考虑一个场景:

ifconfig查询网络信息,我们需要提取ip地址,那么怎么做呢?

[kuerl@fonova-app02 ~]$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:3E:00:04:72
inet addr:10.161.170.72 Bcast:10.161.175.255 Mask:255.255.240.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:10923567134 errors:0 dropped:0 overruns:0 frame:0
TX packets:5664847521 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9099137466328 (8.2 TiB) TX bytes:8427675868771 (7.6 TiB)
Interrupt:154


grep全文搜索命令
DESCRIPTION
   grep searches the named input FILEs (or standard input if no files are named, or if a
   single hyphen-minus (-) is given as file name) for lines containing a  match  to  the
   given PATTERN.  By default, grep prints the matching lines.

根据pattern 匹配行

下面是一些参数:

       -e PATTERN, --regexp=PATTERN
              Use  PATTERN  as  the  pattern.   This  can be used to specify multiple search
              patterns, or to protect a  pattern  beginning  with  a  hyphen  (-).   (-e  is
              specified by POSIX.)

       -f FILE, --file=FILE
              Obtain  patterns  from  FILE,  one  per  line.   The  empty file contains zero
              patterns, and therefore matches nothing.  (-f is specified by POSIX.)

       -i, --ignore-case
              Ignore case distinctions in both the PATTERN and  the  input  files.   (-i  is
              specified by POSIX.)

       -v, --invert-match
              Invert  the sense of matching, to select non-matching lines.  (-v is specified
              by POSIX.)

demo:

匹配有255的行
[kuerl@fonova-app02 ~]$ ifconfig | grep '255'
          inet addr:10.161.170.72  Bcast:10.161.175.255  Mask:255.255.240.0
          inet addr:115.29.170.55  Bcast:115.29.171.255  Mask:255.255.252.0
          inet addr:127.0.0.1  Mask:255.0.0.0

-v 匹配不含有255的行
[kuerl@fonova-app02 ~]$ ifconfig | grep -v '255'
eth0      Link encap:Ethernet  HWaddr 00:16:3E:00:04:72  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10923576144 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5664856656 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9099138431309 (8.2 TiB)  TX bytes:8427679896157 (7.6 TiB)
          Interrupt:154 


-i匹配,不考虑大小写
[kuerl@fonova-app02 ~]$ ifconfig | grep -i 'gib'
          RX bytes:122678729865 (114.2 GiB)  TX bytes:690949671740 (643.4 GiB)
          RX bytes:115773706267 (107.8 GiB)  TX bytes:115773706267 (107.8 GiB)

按照指定的正则pattern 匹配指定的行

[kuerl@fonova-app02 ~]$ ifconfig | grep -e '[a-z][1-9]'
eth1      Link encap:Ethernet  HWaddr 00:16:3E:00:22:44  

sed文本转换命令

DESCRIPTION
Sed is a stream editor. A stream editor is used to perform basic text transforma-
tions on an input stream (a file or input from a pipeline). While in some ways simi-
lar to an editor which permits scripted edits (such as ed), sed works by making only
one pass over the input(s), and is consequently more efficient. But it is sed’s
ability to filter text in a pipeline which particularly distinguishes it from other
types of editors.

一个最常用法sed 文本替换命令,
s/regexp/replacement/
Attempt  to  match  regexp  against the pattern space.  If successful, replace
that portion matched with replacement.  The replacement may contain  the  special  character & to refer to that portion of the pattern space which matched,and the special escapes \1 through \9 to refer to the  corresponding  matching
sub-expressions in the regexp.



demo

BOOTPROTO   static 改为dhcp

[kuerl@fonova-app02 Test]$ cat 123.txt 
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.181.172.79
NETMASK=255.255.240.0

[kuerl@fonova-app02 Test]$ cat 123.txt |sed 's/static/dhcp/g'
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
IPADDR=10.181.172.79
NETMASK=255.255.240.0



awk文本分析工具

demo

用户信息目录

[kuerl@fonova-app02 Test]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
..
.


只获取用户名  其中-F 是用来指定分隔符,$ 应用分割之后的第几列数据,如果0则表示整行。

[kuerl@fonova-app02 Test]$ cat /etc/passwd | awk -F ':' '{print $1}' 
root
bin
daemon
adm
lp
..
.


awk内容比较多,详细可参考https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html

最后说开始的场景,获取网络信息中的ip地址

[kuerl@fonova-app02 Test]# ifconfig | grep 'inet addr' | grep -v '127.0.0.1' | sed 's/inet addr://g'|sed 's/Bcast.*//g'

          192.168.119.135 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值