Get first line of a shell command's output

前端 未结 2 611
情深已故
情深已故 2020-12-23 15:44

While trying to read the version number of vim, I get a lot additional lines which I need to ignore. I tried to read the manual of head and tried t

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 16:22

    I would use:

    awk 'FNR <= 1' file_*.txt
    

    As @Kusalananda points out there are many ways to capture the first line in command line but using the head -n 1 may not be the best option when using wildcards since it will print additional info. Changing 'FNR == i' to 'FNR <= i' allows to obtain the first i lines.

    For example, if you have n files named file_1.txt, ... file_n.txt:

    awk 'FNR <= 1' file_*.txt
    
    hello
    ...
    bye
    

    But with head wildcards print the name of the file:

    head -1 file_*.txt
    
    ==> file_1.csv <==
    hello
    ...
    ==> file_n.csv <==
    bye
    

提交回复
热议问题