Views:
45,589β
Votes: 11β
β
Solution
Tag :
text-processing
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/950434
Title:
How can I copy the content of a text file and paste it to another starting at a certain line?
ID:
/2017/08/27/How-can-I-copy-the-content-of-a-text-file-and-paste-it-to-another-starting-at-a-certain-line_
Created:
August 27, 2017
Edited: June 12, 2020
Upload:
October 19, 2025
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
head and tail solution
Assume the source file is called ~/a and the file to be inserted is called ~/b. Weβll put the merged file into ~/c:
head -n 5 ~/a > ~/c
cat ~/b >> ~/c
tail --lines=+6 ~/a >> ~/c
- The path
~/is short hand for your/home/userdirectory name - head copies the first five lines of file
ainto newly created filec - cat lists the contents of file
band appends it to filec - tail appends file
astarting at line 6 until the end to filec
After verification rename merged file
After verifying that file c is merged correctly from files a and b weβll rename c to a using:
mv ~/c ~/a
mvmoves filecinto filea. Data isnβt physically moved. The file is simply renamed which saves time.