Find and Add line in text file from batch - text

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.

Related

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

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

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.

Get the last line of a file in groovy

Google doesn't give any great results for getting the last line of a file in groovy so I feel this question is necessary.
How does one get the last line of a file in groovy?
Get all lines, then get the last one:
def lines=new File("/home/user/somefile.txt").readLines()
def lastline=lines.get(lines.size()-1)
Or, if the file is dangerously large:
def last=new File('/home/user/somefile.txt').withReader { r-> r.eachLine {it} }
which is almost identical to asker's answer.
Here's what I ended up with:
new File("/home/user/somefile.txt").eachLine {
lastLine = it
}
.eachLine iterates through each line of the text file and lastLine is set to the current iteration until eachLine finishes going through the file. Pretty straightforward.
How does one get the last line of a file in groovy?
It depends on what you know about the file. If the lines in the file are fixed length then you could look at the file size, divide it by the length of each line and then use random file access to jump to the last line in the file and read it. That would be more efficient than reading the entire file. If you don't know the length of the lines then you are probably going to have to read the entire file and discard everything before the last line you read.

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

Search string in multiple files to write lines into one file

I'm relatively new in trying things out in the command prompt.
In the end I'd like to write a batch file that can do the following things.
Look for files whose name does not contain "foo" or "bar" with the extension "txt"
in the current directory, including all sub directories,
and search for lines starting with "create file" or "create table,"
so that all corresponding matching lines would be put into a new file named "y"
In the form of name of file in which the line was found, location of the file, and the actual line containing the string.
Start by looking at these commands.
Commands
for,
find,
findstr
Use the /? argument for help.
Batch References
SS64,
DosTips,
Rob van der Woude,
Technet

Resources