Skip to content
Advertisement

How do I replace all lines after line XX in one file with content from another file?

I’m on Debian 8.2. Here’s test.sh so far.

#!/bin/bash
wget http://winhelp2002.mvps.org/hosts.txt -O fileA
tail -n +26 fileA >> fileB

I want lines 26 onwards of fileA‘s content to replace everything in fileB from line 26 onward — so I end up with the first 25 lines of the output are from lines 1-25 of the original fileB and the remainder is lines 26 onwards of fileA.

How do I do this?

Advertisement

Answer

#!/bin/bash
wget http://winhelp2002.mvps.org/hosts.txt -O fileA
head -25 fileB > tempfile && mv tempfile fileB
tail -n +26 fileA >> fileB

head -25 will take first 25 lines from fileB and dump it to tempfile. Then tempfile will be renamed to fileB.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement