![Linux文本处理命令续:cut、tr、wc 图片[1]-Linux文本处理命令续:cut、tr、wc-不念博客](https://www.bunian.cn/wp-content/uploads/2023/01/image-1.jpeg)
cut命令:文件内容截取
cut OPTION... [FILE]...cut [选项] ... [文件名]选项:-d 以。。为分隔符(这个默认不是空格)注意'的话要用"'"引起来-f 选择分隔后的区域-c:按照字符去截取info.txt内容:I'm xxx,18 years old QQ 133411023I'm ycd,20 years old QQ 123456789cut -d '' -f 6 info.txt133411023123456789cut -d '' -f 6,2 info.txtxxx,18 133411023ycd,20 123456789cut -d '' -f 6,2 info.txt |cut -d ',' -f 218 13341102320 123456789cut -c 1 info.txtIIcut -c 10-11,25-34 info.txt8 1334110230 123456789cut 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 123456789cut 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 7133411023123456789替换的是原来的内容位数必须和新内容位数相同,只能一格换一格 tr '原来的内容' '新内容' < 文件名 cat info.txt | tr "," " "|cut -d ' ' -f 7 133411023 123456789替换的是原来的内容位数必须和新内容位数相同,只能一格换一格 tr '原来的内容' '新内容' < 文件名 cat info.txt | tr "," " "|cut -d ' ' -f 7 133411023 123456789
wc 统计
在默认的情况下,wc将计算指定文件的行数、字数,以及字节数。wc选项wc -l 文件 统计行数wc -w 文件 统计单词数-c 统计字符数例如:[root@sky ~]# wc /etc/passwd19 29 848 /etc/passwd例如:# wc -c /etc/services670293 /etc/services在默认的情况下,wc将计算指定文件的行数、字数,以及字节数。 wc选项 wc -l 文件 统计行数 wc -w 文件 统计单词数 -c 统计字符数 例如: [root@sky ~]# wc /etc/passwd 19 29 848 /etc/passwd 例如: # wc -c /etc/services 670293 /etc/services在默认的情况下,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 610.0.0.251/24[root@localhost ~]# ifconfig eth0 |grep 'inet' |cut -d ' ' -f 1010.0.0.251fe80::e1af:89d9:4338:31df[root@localhost ~]# ifconfig eth0 |grep 'inet' |head -1 |cut -d ' ' -f 1010.0.0.251或[root@sky ~]# ifconfig eth0|grep -w 'inet'|cut -d ' ' -f 1010.0.0.250[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[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