7

I need to append a file in a specific location of another file. I got the line number so, my file is:

file1.txt:

I
am
Cookie

While the second one is

file2.txt:

a
black
dog
named

So, after the solution, file1.txt should be like

I
am
a
black
dog
named
Cookie

The solution should be compatible with the presence of characters like " and / in both files.

Any tool is ok as long as it's native (I mean, no new software installation).

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Wyatt Gillette
  • 318
  • 3
  • 14

3 Answers3

8

Another option apart from what RavinderSingh13 suggested using sed: To add the text of file2.txt into file1.txt after a specific line:

sed -i '2 r file2.txt' file1.txt

Output:

I
am
a
black
dog
named
Cookie

Further to add the file after a matched pattern:

sed -i '/^YourPattern/ r file2.txt' file1.txt
Ardit
  • 1,522
  • 17
  • 23
  • also this one worked, i gave answer to the first due to chronological order :) btw, this means: edit the stream at line 2 inserting file2.txt in file1.txt? Looks even simpler... – Wyatt Gillette Sep 26 '17 at 11:20
  • Means add lines of file2 after the 2nd line of file1. It doesn't invoke `cat` as in the other answer. – Ardit Sep 26 '17 at 11:30
  • yup was easy with 'r' , how could I not think about it – Wyatt Gillette Sep 26 '17 at 11:34
  • Also, it is supposed that the accepted answer is the best answer, not the first anhway :) – Ardit Sep 26 '17 at 11:39
  • This should be the accepted answer since allows one to add the contents of a file either after a specific line number or after a pattern – leoschet Mar 29 '20 at 18:08
3

Could you please try following and let me know if this helps you.

awk 'FNR==3{system("cat file2.txt")} 1' file1.txt

Output will be as follows.

I
am
a
black
dog
named
Cookie

Explanation: Checking here if line number is 3 while reading Input_file named file1.txt, if yes then using system utility of awk which will help us to call shell's commands, then I am printing the file2.txt with use of cat command. Then mentioning 1 will be printing all the lines from file1.txt. Thus we could concatenate lines from file2.txt into file1.txt.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    this worked! To understand your answer... the translation is: when u arrive at line 3 cat file2.txt and then stop? interesting use of system()..thnx! – Wyatt Gillette Sep 26 '17 at 11:14
  • @WyattGillette, you are welcome. Glad that it helped you, I just added explanation of same, kindly do let me know if you have any other queries on same. – RavinderSingh13 Sep 26 '17 at 11:18
2

How about

head -2 file1 && cat file2 && tail -1 file1

You can count the number of lines to decide head and tail parameters in file1 using

wc -l file1
Pero
  • 1,371
  • 17
  • 18
  • 1
    creativity at work here and it's working, good workaround one point is: if the file is huge (ex. 5000+ lines) would be a problem to count tail parameter...easy to find head one (cat -n and grep) one could do: tac file2 > file3 && cat -n file3 | grep dog to find the tail parameter, tough...funny way and working – Wyatt Gillette Sep 26 '17 at 11:40