How can I insert a text file at specific line of another - linux

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.

will be simpler with sed
$ sed '/insert the text file/r methods_to_import.txt' my_api.rb

Related

How to add text to a file in python3

Let's say i have the following file,
dummy_file.txt(contents below)
first line
third line
how can i add a line to that file right in the middle so the end result is:
first line
second line
third line
I have looked into opening the file with the append option, however that adds the line to the end of the file.
with open("dummy_file.txt", 'r') as file:
lines = file.readlines()
lines.insert(1, "second line\n")
with open("dummy_file.txt", 'w') as output:
output.writelines(lines)
So:
We open the file an read all the lines making a list.
We insert to the list the desired new line, using \n for a new line.
We open the file again but this time to write.
We write all the lines from the list.
But I wouldn't recommend this method, due it hight memory usage (if the file is big).
The standard file methods don't support inserting into the middle of a file. You need to read the file, add your new data to the data that you read in, and then re-write the whole file.

using sed to delete lines with multiple matching strings in a single line

I have a file containing multiple lines. Each line has a specific set of keywords or pattern in it. These keywords remain same for all lines. I want to use sed to delete matching lines from a file based on a multiple keywords. So for instance the line in the file will look something like
Begin: Action Title:<could be anything> --Date:<somedate> Value1:<could be anything> Value2:<could be anything>
The file has other irrelavant content as well. How can I use sed to locate the matching lines based on multiple patterns such as Begin: Action, Title: as well as Date: or Value1. I can use a single pattern/keyword to identify the matching lines but to be safe I want to use all the keywords/patterns in a line to make sure I do not delete the other lines.
So i am doing
sed -n '/Begin: Action/,/Date:/,/Value1:/d' File
But its not working.

Print out file lines with special characters - Linux

I have a txt file with that is formatted like this:
xxxxxxxxx,xxx
with a few thousand lines.
I have this command right now to delete the special characters from the file
sed -i -e:a -e 's/[^0-9]\(.*,\)/\1/;ta' file.txt
instead of editing the actual file, is there a way to print out the line of text that each individual one lies on?
I only need to test the first 9 characters as every line has a comma in it. :)
It SOUNDS like all you want is:
grep '[^[:alnum:]]' file
but without sample input/output we're just guessing.

Paste header line in multiple tsv (tab separated) files

I have multiple .tsv files named as choochoo1.tsv, choochoo2.tsv, ... choochoo(nth).tsv files. I also have a main.tsv file. I want to extract the header line in main.tsv and paste over all choochoo(nth).tsv files. Please note that there are other .tsv files in the directory that I don't want to change or paste header, so I can't do *.tsv and select all the .tsv files (so need to select choochoo string for wanted files). This is what I have tried using bash script, but could not make it work. Please suggest the right way to do it.
for x in *choochoo; do
head -n1 main.tsv > $x
done
You have a problem with the file glob, as well as the redirect:
the file glob will catch things like AAchoochoo but not choochoo1.tsv and not even AAchoochoo.tsv
the redirect will overwrite the existing files instead of adding to them. The redirect command for adding to a file is >>, but that will append text to the end and you want to prepend text in the beginning.
The problem with prepending text to an existing file, is that you have to open the file for both reading and writing and then stream both prepended text and original text, in order - and that is usually where people fail because the shell can't open files like that (there is a slightly more complex way of doing this directly, by opening the file for both reading and writing, but I'm not going to address that further).
You might want to use a temporary file, something like this:
for x in choochoo[0-9]*.tsv; do
mv "$x"{,.orig}
(head -n1 main.tsv; cat "$x.orig") > $x
rm "$x.orig"
done

Find and Add line in text file from batch

I would like to know if anyone can help me with this. I would like to search a text file for a certain line of text, add a new line under the specific line then add text to the new line. I will be using this to edit the firefox.js file to add the line of text to add support for Iprism. It will run on XP and Windows 7 machines.
I would like to have a batch file that will open firefox.js find the line "pref("browser.xul.error_pages.expert_bad_cert", false);" add a new line uder it and add
pref("network.automatic-ntlm-auth.trusted-uris", "IP of Iprsim");
Edited for better explanation!!
Any help would be very appreciated!!!
Damian
You can iterate over lines in a file with for /f. You need to keep track of the line you're currently at, compare it to what you look for and in case you found the line you were looking for do something. This something looks roughly as follows: You output each line you process to stdout or a new file directly and when you find your desired line you do exactly the same but write something else to that new file as well. At the very end you simply delete your old input file and rename the new one.
In a batch file this might look somehow like the following (untested, so careful):
for /f %%x in (inputfile) do (
echo %%x>>newfile
if ("%%x"=="Ex3") (
echo Ex4>>newfile
)
)
del inputfile
ren newfile inputfile
Of course, adapt as you see fit.

Resources