【问题标题】:Using head and tail command in unix to extract items from a file在 unix 中使用 head 和 tail 命令从文件中提取项目
【发布时间】:2018-12-21 13:35:38
【问题描述】:

我目前是 unix 过滤器领域的新手,并且只介绍了 unix 中的基本命令。

我的 ubuntu 中有一个文件 emp.lst,其中包含以下详细信息

cat emp.lst

2233|a.k.shukla|g.m.|sales|12/12/52|6000
9876|jai sharma|direcor|production|12/03/50|7000
5678|sumit chakraborty|d.g.m|marketing|19/04/43|6000
2365|barun sengupta|director|personnel|11/05/47|7800
5423|n.k. gupta|chairman|admin|30/08/56|5400
1006|chanchal singhvi|director|sales|03/09/38|6700
6213|karuna ganguly|g.m.|accounts|05/06/62|6300
1265|s.n. dasgupta|manager|sales|12/09/63|5600
4290|jayant choudhary|executive|production|07/09/50|6000
2476|anil aggarwal|manager|sales|01/05/59|5000
6521|lalit chowdary|director|marketing|26/09/45|8200
3212|shyam saksena|d.g.m.|accounts|12/12/55|6000
3564|sudhir aggarwal|executive|personnel|06/07/47|7500
2345|j.b.saxena|g.m.|marketing|12/03/45|8000
0110|v.k. agrawal|g.m.|marketing|31/12/40|9000

现在,我明白了 head 提取该文件的顶部内容,而 tail 用于访问底部内容。

$head -3 emp.lst returns the first 3 rows in the file emp.lst

同样, $tail -2 emp.lst 返回文件中的最后 2 行

现在,如果我要一起提取它们,即从头部提取前 3 行,从尾部提取最后 2 行?无论如何,这可以实现吗?

我也试过 $head -3 emp.lst; tail -2 emp.lst \这达到了所需的结果,但我认为;仅用于在一行中键入多个命令 还有 $head -3 emp.lst | tail -2 不起作用。

【问题讨论】:

    标签: unix ubuntu-14.04


    【解决方案1】:

    你可以试试

    (head -3; tail -2) < emp.lst
    

    如果你想验证你可以做的行号

    cat emp.lst | nl | (head -3; tail -2) 
    

    【讨论】:

    • 有效!很好,谢谢楼主
    • @Dominique 我现在还没有准备好 WSL,但是当我回到家时我可以测试它。它适用于我的 suse 和 redhat 容器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2021-01-30
    • 2017-03-28
    • 2015-12-11
    • 1970-01-01
    • 2010-09-16
    相关资源
    最近更新 更多