linux系统中批量提取指定行的数据
2021/7/22 7:06:32
本文主要是介绍linux系统中批量提取指定行的数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、测试数据
[root@centos79 test]# cat a.txt 01 3 4 02 f s 03 t 3 04 s d 05 g i 06 y e 07 a w 08 d g 09 e w 10 n u 11 7 8 12 3 s 13 a d 14 g e 15 w a 16 z v 17 n m 18 k p
2、提取 3,5,7,8,9,15,17行
使用sed
[root@centos79 test]# ls a.txt lines [root@centos79 test]# cat lines 3 5 7 8 9 15 17 [root@centos79 test]# cat a.txt 01 3 4 02 f s 03 t 3 04 s d 05 g i 06 y e 07 a w 08 d g 09 e w 10 n u 11 7 8 12 3 s 13 a d 14 g e 15 w a 16 z v 17 n m 18 k p [root@centos79 test]# for i in `cat lines`; do sed -n "$i"p a.txt >> result; done [root@centos79 test]# ls a.txt lines result [root@centos79 test]# cat result 03 t 3 05 g i 07 a w 08 d g 09 e w 15 w a 17 n m
使用awk
[root@centos79 test]# ls a.txt lines [root@centos79 test]# cat a.txt 01 3 4 02 f s 03 t 3 04 s d 05 g i 06 y e 07 a w 08 d g 09 e w 10 n u 11 7 8 12 3 s 13 a d 14 g e 15 w a 16 z v 17 n m 18 k p [root@centos79 test]# cat lines 3 5 7 8 9 15 17 [root@centos79 test]# for i in $(cat lines ); do awk -v a=$i 'NR == a' a.txt >> result; done [root@centos79 test]# cat result 03 t 3 05 g i 07 a w 08 d g 09 e w 15 w a 17 n m
使用while语句:
[root@centos79 test]# ls a.txt lines [root@centos79 test]# cat a.txt 01 3 4 02 f s 03 t 3 04 s d 05 g i 06 y e 07 a w 08 d g 09 e w 10 n u 11 7 8 12 3 s 13 a d 14 g e 15 w a 16 z v 17 n m 18 k p [root@centos79 test]# cat lines 3 5 7 8 9 15 17 [root@centos79 test]# cat lines | while read i; do sed -n "$i"p a.txt >> result; done [root@centos79 test]# cat result 03 t 3 05 g i 07 a w 08 d g 09 e w 15 w a 17 n m
while + awk
[root@centos79 test]# ls a.txt lines [root@centos79 test]# cat a.txt 01 3 4 02 f s 03 t 3 04 s d 05 g i 06 y e 07 a w 08 d g 09 e w 10 n u 11 7 8 12 3 s 13 a d 14 g e 15 w a 16 z v 17 n m 18 k p [root@centos79 test]# cat lines 3 5 7 8 9 15 17 [root@centos79 test]# cat lines | while read i; do awk -v a=$i 'NR == a' a.txt >> result; done [root@centos79 test]# cat result 03 t 3 05 g i 07 a w 08 d g 09 e w 15 w a 17 n m
这篇关于linux系统中批量提取指定行的数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-11Linux部署Scrapy学习:入门级指南
- 2024-09-11Linux部署Scrapy:入门级指南
- 2024-08-21【Linux】分区向左扩容的方法
- 2024-08-21【Linux】gnome桌面环境切换KDE Plasma
- 2024-08-19如何安装 VMware Tools (macOS, Linux, Windows)
- 2024-08-15Linux部署Scrapy教程:入门级指南
- 2024-07-29linux命令行下好用的性能监控工具atop
- 2024-07-04Linux系统上离线升级SSH服务的具体操作步骤-icode9专业技术文章分享
- 2024-06-0600-macOS和Linux安装和管理多个Python版本
- 2024-03-30[译]漫画SELinux概念