首先介绍一个并不常用的命令:nl
nl命令最基本的用法:为你将要查看的文件或者标准输出添加行号
;
但务必注意的是:nl若不添加
其他参数
将忽略空行
,不对其做行号递增排序显示;
此外,nl 与cat -n 作用类同
例如:
[root@C7-3 ~]# cat /data/f1
a
b
[root@C7-3 ~]# nl /data/f1
1 a
2 b
[root@C7-3 ~]# cat -n /data/f1
1 a
2
3 b
[root@C7-3 ~]# nl -b a /data/f1
1 a
2
3 b
计算/etc/passwd 中第10行与第20行 uid之和
nl /etc/passwd|egrep "^ +10|20"|awk -F: '{print $3}'|sed -nr '1~2N;s/\n/+/p'|bc
nl命令仅做介绍到这;
对于内容或者行号很多的文件来说,使用nl 与sed 组合可以快速将我们想要的某行或某几行定位出来;
例如:
在我的passwd 文件中有个ftp用户
我并不知道该用户信息在第几行,而我又想将该用户信息取出单独输出至终端
[root@C7-3 ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
.................
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
.....................
[root@C7-3 ~]# nl /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
....................
11 games:x:12:100:games:/usr/games:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
..........................
[root@C7-3 ~]# nl /etc/passwd|sed -n '12p'
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
很快我们就将该用户信息找到并单独打印
熟练使用各种基础命令掌握多种方法能够使我们的工作有更多其他选择
最后例举几个`CentOS / Ubuntu` 通用 `取出主机IP` 的命令作为结束吧
ip a|sed -n '9p'|awk -F"/| " '{print $6}'
ip a|nl|grep "\b9\b"|tr -s " "|cut -d" " -f 4|tr '/' '\n'|head -1
ip a|egrep "([0-9]){3}\.([0-9]){3}"|awk -F" |/" '{print $6}'
[root@C7-3 ~]# ip a|sed -n '9p'|awk -F"/| " '{print $6}'
10.20.0.13
[root@C7-3 ~]# ip a|nl|grep "\b9\b"|tr -s " "|cut -d" " -f 4|tr '/' '\n'|head -1
10.20.0.13
[root@C7-3 ~]# ip a|egrep "([0-9]){3}\.([0-9]){3}"|awk -F" |/" '{print $6}'
10.20.0.13