Reputation: 1789
I intend to copy some lines of a file to a specified location in another file using shell scripts. I only know a command:
cat A >> B
But, it only works for copying all lines of file A and paste them to the end file B. Instead, I want to insert content from file A in the middle of the file B.
File A:
aaa
bbb
ccc
ddd
File B:
AAA
BBB
CCC
Then copy lines 2-3 of file A to the file B after line 2:
AAA
BBB
bbb
ccc
CCC
In other words, I want to insert some successive lines of a file into another file at any point. How can I do that?
Upvotes: 0
Views: 511
Reputation: 8571
I am not sure about any command but I have workaround for it,
If you know line no at which you want to add something then why dont you use head and tail commands
suppose you want to aaa in your some file A at line no 3 A is like,
AAA
BBB
CCC
DDD
then
head -n 2 > B # this will copy first 2 lines
echo "aaa" >> B # your desired string at line no 3
tail -n 2 >>B # remaining lines into file
instead of tail you can try
'sed 1,2d' A >> B
or
awk 'NR>2' A >> B
for writing remaining lines to B
This can be used for smaller files but will be heavy for larger files
Upvotes: 2
Reputation: 46990
Well, if you have the standard Linux tools, you can say
paste -d "\n" A B
Upvotes: 0