Using awk to pull specific lines from a file

前端 未结 6 2045
醉话见心
醉话见心 2020-12-03 15:36

I have two files, one file is my data, and the other file is a list of line numbers that I want to extract from my data file. Can I use awk to read in my lines file, and the

6条回答
  •  醉梦人生
    2020-12-03 16:05

    awk 'NR == FNR {nums[$1]; next} FNR in nums' numberfile datafile
    

    simply referring to an array subscript creates the entry. Looping over the first file, while NR (record number) is equal to FNR (file record number) using the next statement stores all the line numbers in the array. After that when FNR of the second file is present in the array (true) the line is printed (which is the default action for "true").

提交回复
热议问题