Linux文本处理命令续:cut、tr、wc

图片[1]-Linux文本处理命令续:cut、tr、wc-不念博客

cut命令:文件内容截取

cut OPTION... [FILE]...
cut [选项] ... [文件名]
选项:
-d 以。。为分隔符(这个默认不是空格)注意'的话要用"'"引起来
-f 选择分隔后的区域
-c:按照字符去截取


info.txt内容:
I'm xxx,18 years old QQ 133411023
I'm ycd,20 years old QQ 123456789


cut -d '' -f 6 info.txt
133411023
123456789


cut -d '' -f 6,2 info.txt
xxx,18 133411023
ycd,20 123456789


cut -d '' -f 6,2 info.txt |cut -d ',' -f 2
18 133411023
20 123456789


cut -c 1 info.txt
I
I


cut -c 10-11,25-34 info.txt
8 133411023
0 123456789

tr 替换

替换的是原来的内容位数必须和新内容位数相同,只能一格换一格


tr '原来的内容' '新内容' < 文件名


cat info.txt | tr "," " "|cut -d ' ' -f 7
133411023
123456789

wc 统计

在默认的情况下,wc将计算指定文件的行数、字数,以及字节数。
wc选项
wc -l 文件   统计行数
wc -w 文件   统计单词数
   -c       统计字符数


例如:
[root@sky ~]# wc /etc/passwd
19  29 848 /etc/passwd
例如:
# wc -c /etc/services
670293 /etc/services

例题:在ip a输出中截取eth0网卡的IP地址

思路:先grep后cut

[root@localhost ~]# ip a |grep 'eth0$' |cut -d ' ' -f 6
10.0.0.251/24
[root@localhost ~]# ifconfig eth0 |grep 'inet' |cut -d ' ' -f 10
10.0.0.251
fe80::e1af:89d9:4338:31df


[root@localhost ~]# ifconfig eth0 |grep 'inet' |head -1 |cut -d ' ' -f 10
10.0.0.251
或
[root@sky ~]# ifconfig eth0|grep -w 'inet'|cut -d ' ' -f 10
10.0.0.250
© 版权声明
THE END
喜欢就支持一下吧
点赞150赞赏 分享
评论 抢沙发
头像
欢迎光临不念博客,留下您的想法和建议,祝您有愉快的一天~
提交
头像

昵称

取消
昵称代码图片

    暂无评论内容