zoukankan      html  css  js  c++  java
  • Linux中一个文件10行内容,如何输出5-8内容到屏幕

    题目是这样的,Linux中一个文件10行内容,如何输出5-8内容到屏幕
    首先我们模拟一下这样的环境:

    [root@localhost question]# pwd
    /root/question
    [root@localhost question]# seq 1 10 > q.txt 
    [root@localhost question]# cat q.txt 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    我们的任务是取5-8行输出:

    第一种方法:

    [root@localhost question]# sed -n '5,8p' q.txt 
    5
    6
    7
    8

    第二种方法:

    [root@localhost question]# awk 'NR>4 && NR <9' q.txt 
    5
    6
    7
    8

    第三种方法:

    [root@localhost question]# grep 5 q.txt  -A 3
    5
    6
    7
    8

    第四种方法:

    [root@localhost question]# grep 8 q.txt -B 3
    5
    6
    7
    8

    其本质就是用grep,awk,sed这Linux三剑客来实现,更想说的是这三个命令可以说是Linux基础命令的核心。

  • 相关阅读:
    Elasticsearch
    Docker
    Python 目录
    淘宝
    MyBatis
    Docker 安装ubuntu服务器
    goodrain云平台 mysql主从同步应用创建
    flask入门
    virtualenv
    进程 线程(二)
  • 原文地址:https://www.cnblogs.com/heqiuyu/p/10372062.html
Copyright © 2011-2022 走看看