Replace first few lines with first few lines from other file

后端 未结 3 400
夕颜
夕颜 2020-12-18 10:10

I am working on Linux. I have 2 files - file1.dat and file2.dat.

cat file1.dat
1
2
3
4
5
6
7
8
9
10

and for file2:

cat file         


        
3条回答
  •  醉酒成梦
    2020-12-18 10:30

    awk is your friend

    Script

    # awk 'NR==FNR && FNR<=3 || NR>FNR && FNR>4' file2 file1
    

    Output

    1a
    2a
    3a
    5
    6
    7
    8
    9
    10
    

    Tips

    • NR - Total number of records processed
    • FNR - Total number of records processed but resets when reading a new file.
    • When a condition evaluates to true and no extra commands are given,awk just prints.

    All good :-)

提交回复
热议问题