How to copy data from a file to another file, starting from a specific line

I have two files, data.txtand results.txtif data.txtthere are 5 lines in it, I want to copy all these lines and paste them into the file results.txt, starting with line number 4 .

The following is an example:

Data.txt File:

stack
ping
dns
ip
remote

Results.txt file:

# here are some text
# please do not edit these lines
# blah blah..
this is the 4th line that data should go on.

I tried sedwith various combinations, but I could not get it to work, I am not sure if it is suitable for this purpose.

sed -n '4p' /path/to/file/data.txt > /path/to/file/results.txt

The above code only copies line 4. This is not what I'm trying to achieve. As I said above, I need to copy all the lines from data.txtand paste them in results.txt, but it should start at line 4 without changing or overriding the first three lines.

Any help is greatly appreciated.

EDIT:

, 4 results.txt. , 3 data.txt.

+4
4

, cron. :

# preserve first lines of results
head -3 results.txt > results.TMP

# append new data
cat data.txt >> results.TMP

# rename output file atomically in case of system crash
mv results.TMP results.txt
+4

, cat a fifo, :

cat <(head -3 result.txt) data.txt > result.txt
+2
head -n 3 /path/to/file/results.txt > /path/to/file/results.txt
cat /path/to/file/data.txt >> /path/to/file/results.txt
+1

awk:

awk 'NR!=FNR || NR<4' Result.txt Data.txt
+1

Source: https://habr.com/ru/post/1621530/


All Articles