多个shell命令提取部分文件的方法介绍

sed、tac、head、tail 用法

#删除前10行
sed -i '1,10d' filename

#删除第10行
sed -i '10d' filename

#删除后两行
sed -i '$d' filename
sed -i '$d' filename

#不显示最后两行
tac filename | sed '1,2d' | tac

#展示第5行到第10行
sed -n ‘5,10p’ filename

#展示前10行
head -10 filename
head -n 10 filename
head -n -10 filename

#展示后10行
tail -n 10 filename
tail -10 filename

#带变量的用法,展示第6行到文件倒数第4行
num=`cat fliename |wc -l`
sed -n "5,$((num-3))p" filename
sed -n "5,$[num-3]p" filename

awk用法

#输出文件第四列
cat filename |awk ‘{print $4}’

cut用法

cut -c “5-15” filename (输出第5~15之间的字符)
cut -b  “5-15” filename

-b:仅显示行中指定直接范围的内容;
-n:与“-b”选项连用,不分割多字节字符;
-c:仅显示行中指定范围的字符;
-d:指定字段的分隔符,默认的字段分隔符为“TAB”;
-f:显示指定字段的内容
–complement:补足被选择的字节、字符或字段;
–out-delimiter=<字段分隔符>:指定输出内容是字段分割符;
–help:显示指令的帮助信息;
–version:显示指令的版本信息。

split用法

[root@server 1-1]# du -sh test.qcow2 
196K    test.qcow2
[root@server 1-1]# split -b 100k test.qcow2 
[root@server 1-1]# ls
test.qcow2  xaa  xab
[root@server 1-1]# split -6 1.txt 
[root@server 1-1]# ls
1.txt  xaa  xab
[root@server 1-1]# cat xaa 
Starting Nmap 7.92 ( https://nmap.org ) at 2022-05-01 21:12 CST
Initiating ARP Ping Scan at 21:12
Completed ARP Ping Scan at 21:12, 0.06s elapsed (1 total hosts)
Initiating SYN Stealth Scan at 21:12
38292/tcp closed landesk-cba      reset ttl 64
MAC Address: 8A:A5:1E:ED:B8:51 (Unknown)
[root@server 1-1]# cat xab 

Read data files from: /usr/bin/../share/nmap
Nmap done: 1 IP address (1 host up) scanned in 4.30 seconds
           Raw packets sent: 1959 (86.180KB) | Rcvd: 44 (2.096KB)
[root@server 1-1]# 

-<行数> : 指定每多少行切成一个小文件
-b<字节> : 指定每多少字节切成一个小文件
-C<字节> : 与参数"-b"相似,但是在切 割时将尽量维持每行的完整性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值