0

I am dynamically generating some SDK code for multiple API sets and need to merge in subsets of files so that my ending import structure works.

I have a file, lets call it methods_to_import.txt of which I have already extractimed the lines I need to insert using:

awk '/method_imports_im_matching/{print}' file_i_need_to_copy_methods_from.rb > method_to_import.txt

I need to insert methods_to_import.txt at a specific line of the main file my_api.rb which looks something like like:

# Models
require 'models/account'
require 'models/account_type'
...

// the line I need to insert the text file into ( ex. line 10 )

# APIs
require 'my_api'

module My Class
  ...
end

I've figured out ways to overwrite the file completely: echo "foo" > bar.txt

or Insert a single string at a specific file line: awk 'NR==3{print "Single String, but not the entire file output :("}1' my_api.rb

https://unix.stackexchange.com/questions/271475/insert-text-at-specific-line-number

Perhaps I need to loop each line of the .txt file and insert line by line into the .rb file?

But I cant figure out how to insert the entire text file at a specific line of the existing file. I'm pretty sure it can be done with a combination of sed || awk. Thanks in advance.

SerKnight
  • 2,502
  • 1
  • 16
  • 18

1 Answers1

4

will be simpler with sed

$ sed '/insert the text file/r methods_to_import.txt' my_api.rb
karakfa
  • 66,216
  • 7
  • 41
  • 56