Using awk to pull specific lines from a file

前端 未结 6 2058
醉话见心
醉话见心 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:09

    I had the same problem. This is the solution already posted by Thor:

    cat datafile \
    | awk 'BEGIN{getline n<"numbers"} n==NR{print; getline n<"numbers"}'
    

    If like me you don't have a numbers file, but it is instead passed on from stdin and you don't want to generate a temporary numbers file, then this is an alternative solution:

    cat numbers \
    | awk '{while((getline line<"datafile")>0) {n++; if(n==$0) {print line;next}}}'
    

提交回复
热议问题