Views:
44,178β
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:
September 15, 2024
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/user
directory name - head copies the first five lines of file
a
into newly created filec
- cat lists the contents of file
b
and appends it to filec
- tail appends file
a
starting 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
mv
moves filec
into filea
. Data isnβt physically moved. The file is simply renamed which saves time.