How to append text to existing line in a text file - node.js

I need to append text to an existing line in a systemd unit file using NodeJS. The line I need to edit will always start with ExecStart but the line number it occurs on may not always be the same.
So the below example:
ExecStart=/opt/binaries/my-binary.bin
Now needs to be:
ExecStart=/opt/binaries/my-binary.bin --port=5000 --host=localhost
I have the text to be appended in a constant called serviceArguments which is determined by a separate function.

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.

How to append data in a PS file?

I have the following line in a PS file
version
!cd "//''"
the another file contains the following line
remove
I want to append data from another file in between the single quotes of the above line. After Appending the characters(remove) from another the ps file should contain the following data
version
!cd "/remove/''"
I tried using OUTREC but it din work, the characters versions also got changed
SORT FIELDS=COPY
OUTREC FIELDS=(1:C'!cd "/',
6:1,6,
12:C''"')
Your FIELDS (which is better as BUILD) should be conditional to avoid processing every line.
You've not mentioned how you are getting the data from the other file.
Best would be two steps. Step 1, to create a "symbol file" (SYMNAMES DD when it is used in the second step) to take the data from your second file and give it a name.
Then in the second step, with conditional processing (IFTHEN=(WHEN=(logicalexpression)) to use the value of the symbol as the insertion.
I'm assuming your second file can sometimes contain different values? If not, why not just generate the whole thing? Or use your editor?

Bash Scripting: Find strings on one line of code and insert on own line

I'm trying to write a small bash script that:
-wget's an html file every [x] minutes from the web
-uses some linux utility to find differences in the file between the last two updates
-Uses sed to modify the lines on which new text was detected
The problem I am running into is that the HTML file uses in-line CSS
to format a table, but the actual code for the page is stored on one long line.
Effectively I need a Linux utility that can scan through a single line of code, find
every instance of text between each tags, and insert those instances on their own line. That should make scanning the text easier. Every tool I've tried searches on a per-line basis which can't do what I need since the entire code is stored on a single line.
You could first split the content into lines, by substituting (say) > with >\n. That will break up the document on the end of each HTML tag.
Maybe you don't even need to do that: if you use awk's RS variable to define the record separator as ">" instead of newline. See this page for an example of using RS: http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

Tried to read a text file line by line. But line is split into two and getting stored on next line

I have a text file with special characters as well as normal characters. I am trying to read this file line by line. I have used
string[] lines = System.IO.File.ReadAllLines("Trial.txt");
To read it.
I used a break point and tried to find out the values stored in those lines. It broke some of the lines in between without finishing reading it and stored the rest in a new next line. When I checked the records, I found that the breaking occurs only at the point where there are special characters even though it doesn't happen with a particular special character. If the file has a total of 10 lines and if there is 1 line which has this problem, it reads a total of 11 lines. Can any of you guys pleas help me out with this? The text file is in UTF-8 format.
The File.ReadAllLines method splits the file on carriage return ('\r'), new line ('\n'), or a carriage return followed by a new line (taken from here: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx).
Check if the line that is not supposed to be split has either of those characters (judging from your reply to Luke Wyatt you probably have a new line ('\n') on that line at the point where it splits).

Resources