bash/sed - adding multiple lines at specific location in file - linux

I want to automate adding new backends to a configuration file and can't figure out how to do it. The relevant part of the file looks like this:
[backend]
backends = backend-1 backend-2
timeout = 10
connectionsperhost = 100
[backend-1]
url = https://somedomain.com
secret = 6135bcb7849ca1886e2de193
[backend-2]
url = https://anotherdomain.com
secret = 75048ad646a5af787cbbaaa3
I'm looking for a simple solution to add another backend entry with a specific url and secret.
[backend-3]
url = https://adiffrentdomain.com
secret = 42349234238423424
and at the same time append the name "backend-3" to the line "backends = " at the "[backend]" section.
Is there a way to make this work with sed or bash scripting in genereal?
kindly appreciate any help or hints!

With awk for an arbitrary new backend entry with newbackendname, url and secret being the only provided inputs. Simply adds the new backend to the backends =-list and the corresponging block directly after the general [backend]-block.
awk -vnewentry="newbackendname" -vurl="http://new.url" -vsecret="secret123" '
#in backends= line: add new entry (at first position)
#and set `add` marker
/^backends =/{sub(/^backends = /,"&"newentry" ",$0) ; add=1}
#marker exists and empty line (end of header block) is found:
#print new entry and unset marker `add`
add && /^$/{ print "\n["newentry"]"
print "url = "url
print "secret = "secret
add=0}
#print by default
1' infile.txt
Hopefully the comments make it easy to follow. With this code, you may as well just add url and secret via shell variables as awk -vurl="$URL" -vsecret="$SECRET" -vnewentry="$NEWBACKEND" '...' infile.txt

You can try an approach with awk. It adds a backends entry to lines starting with backends and attaches a [backend-x] section with the given info after the last line of the [backend] block (after connectionsperhost).Obviously it relies on the entries of the backends being correct since it doesn't count the [backend] sections individually.
url="www.domain.com"
s=9283283h4923h42934
% awk -v url="${url}" -v s="${s}" '/^backends/{x=NF-1; $0=$0" backend-"x}
/^connectionsperhost/{ print; $0="\n[backend-"x"]\nurl = "url"\nsecret = "s}
{print}' file
[backend]
backends = backend-1 backend-2 backend-3
timeout = 10
connectionsperhost = 100
[backend-3]
url = www.domain.com
secret = 9283283h4923h42934
[backend-1]
url = https://somedomain.com
secret = 6135bcb7849ca1886e2de193
[backend-2]
url = https://anotherdomain.com
secret = 75048ad646a5af787cbbaaa3
Data
cat file
[backend]
backends = backend-1 backend-2
timeout = 10
connectionsperhost = 100
[backend-1]
url = https://somedomain.com
secret = 6135bcb7849ca1886e2de193
[backend-2]
url = https://anotherdomain.com
secret = 75048ad646a5af787cbbaaa3

Using sed
$ sed '/backends =/s/$/ backend-3/;/^\[backend-2/{N;N;s|secret.*|&\n\n[backend-3] \
url = https://adifferentdomain.com \
secret = 42349234238423424|}' input_file
[backend]
backends = backend-1 backend-2 backend-3
timeout = 10
connectionsperhost = 100
[backend-1]
url = https://somedomain.com
secret = 6135bcb7849ca1886e2de193
[backend-2]
url = https://anotherdomain.com
secret = 75048ad646a5af787cbbaaa3
[backend-3]
url = https://adiffrentdomain.com
secret = 42349234238423424
If the input is coming from variables, you would want to use double quotes instead to expand the variables. E.g;
sed "/backends =/s/$/ backend-3/;$a\\n[backend-3] \
url = $url_input \
secret = $secret_input" input_file

Related

How to programmatically retrieve the workspace url and clusterOwnerUserId?

I would like to programmatically create the url to download a file.
To do this I need the workspaceUrl and clusterOwnerUserId.
How can I retrieve those in a Databricks notebook?
# how to get the `workspaceUrl` and `clusterOwnerUserId`?
tmp_file = '/tmp/output_abcd.xlsx'
filestore_file = '/FileStore/output_abcd.xlsx'
# code to create file omitted for brevity ...
dbutils.fs.cp(f'file:{tmp_file}', filestore_file)
downloadUrl = f'https://{workspaceUrl}/files/output_abcd.xlsx?o={clusterOwnerUserId}'
displayHTML(f"<a href='{downloadUrl}'>download</a>")
The variables are available in the spark conf.
E.g.
clusterOwnerUserId = spark.conf.get('spark.databricks.clusterUsageTags.orgId')
workspaceUrl = spark.conf.get('spark.databricks.workspaceUrl')
Use can then use the details as follows:
tmp_file = '/tmp/output_abcd.xlsx'
filestore_file = '/FileStore/output_abcd.xlsx'
# code to create file omitted for brevity ...
dbutils.fs.cp(f'file:{tmp_file}', filestore_file)
downloadUrl = f'https://{workspaceUrl}/files/output_abcd.xlsx?o={clusterOwnerUserId}'
displayHTML(f"<a href='{downloadUrl}'>download</a>")
Databricks Files in the Filestore at
/FileStore/my-stuff/my-file.txt is accessible at:
"https://databricks-instance-name.cloud.databricks.com/files/my-stuff/my-file.txt"
I don't think you need the o=... part. That is the workspace Id btw, not the clusterOwner user id.

python3 : append string to every list

I am using python3 with gitpython and generating the result as shown below :
0bf35c4cf243e0fe13adbe7aeba99a03ddf6acfd refs/release/17.xp.0.95/head
d0c5f748e65488ce2e90c1ed027c2da252a5c6a2 refs/release/17.xp.0.96/head
530bdbf8f06859d8aca55cee7b57e27e68e87a94 refs/release/17.xp.0.97/head
0dd0342466540bc38e26ef74af6c8837d165cae5 refs/release/17.xp.0.98/head
919b78fb737b00830a8e48353b0f977c442600dd refs/release/17.xp.0.99/head
But i want to append the string name "acme" to every line, for example
0bf35c4cf243e0fe13adbe7aeba99a03ddf6acfd refs/release/17.xp.0.95/head
acme
d0c5f748e65488ce2e90c1ed027c2da252a5c6a2 refs/release/17.xp.0.96/head
acme
530bdbf8f06859d8aca55cee7b57e27e68e87a94 refs/release/17.xp.0.97/head
acme
0dd0342466540bc38e26ef74af6c8837d165cae5 refs/release/17.xp.0.98/head
acme
919b78fb737b00830a8e48353b0f977c442600dd refs/release/17.xp.0.99/head
acme
Below is the code i am using, please advise the solution to append/concatenate the string to every end of the lines.
import os,re,sys,argparse
import git
if len(sys.argv) < 2:
print('Usage : --track <track name> without "track/" ')
sys.exit()
input_track = sys.argv[1].strip()
print ("Checking for the track name - track/",input_track)
def show_ref(input_track,gitname):
url = "git#github/"+gitname+".git"
g = git.cmd.Git()
ig1 = g.ls_remote(url,"refs/heads/track/"+input_track).split('\d')
print ("Branch for glide-test:\n",'\n'.join(ig1))
for x in range(13,20):
ig6 = g.ls_remote(url,"refs/release/"+str(x)+"."+input_track+".*/head").split('|')
print ('\n'.join(ig6))
#"\n".join(map(lambda word: word+"x", s.split("\n")))
show_ref(input_track,"acme")
You can simply modify this line
ig6 = g.ls_remote(url,"refs/release/"+str(x)+"."+input_track+".*/head").split('|')
By adding the string "acme" to the string you build.
Like this
ig6 = g.ls_remote(url,"refs/release/"+str(x)+"."+input_track+".*/head acme").split('|')
Is that what you meant?

How to get the name of the directory from the name of the directory + the file

In an application, I can get the path to a file which resides in a directory as a string:
"/path/to/the/file.txt"
In order to write another another file into that same directory, I want to change the string "/path/to/the/file.txt" and remove the part "file.txt" to finally only get
"/path/to/the/"
as a string
I could use
string = "/path/to/the/file.txt"
string.split('/')
and then glue all the term (except the last one) together with a loop
Is there an easy way to do it?
You can use os.path.basename for getting last part of path and delete it with using replace.
import os
path = "/path/to/the/file.txt"
delete = os.path.basename(os.path.normpath(path))
print(delete) # will return file.txt
#Remove file.txt in path
path = path.replace(delete,'')
print(path)
OUTPUT :
file.txt
/path/to/the/
Let say you have an array include txt files . you can get all path like
new_path = ['file2.txt','file3.txt','file4.txt']
for get_new_path in new_path:
print(path + get_new_path)
OUTPUT :
/path/to/the/file2.txt
/path/to/the/file3.txt
/path/to/the/file4.txt
Here is what I finally used
iter = len(string.split('/'))-1
directory_path_str = ""
for i in range(0,iter):
directory_path_str = directory_path_str + srtr.split('/')[i] + "/"

how to read different block setting from kinto.ini file

I created a different block in my kinto.ini file and i want to use those setting in my program.
#kinto.ini
[mysetting]
name = json
username = jsonmellow
password = *********
[app:main]
use = egg:kinto
kinto.storage_url = postgre//
if we use 'config.get_setting' function of kinto it gives me the setting of the default block "app:main" only. so how can i get the other setting from "mysetting" block.
you can use prefix for your settings like:
[app:main]
...
mysetting.name = json
mysetting.username = jsonmellow
But if you still need some extra section in ini: How can I access a custom section in a Pyramid .ini file?

Can Matlab eliminate the path in URL and left only the domain part?

Can Matlab eliminate the path in URL and leave only the domain part? Does Matlab have any function to eliminate the path behind?
Let's say, example 1:
input :http://www.mathworks.com/help/images/removing-noise-from-images.html
output :http://www.mathworks.com
This regexp pattern should do the trick:
>> str = 'http://www.mathworks.com/help/images/removing-noise-from-images.html';
>> out = regexp(str,'\w*://[^/]*','match','once')
out =
'http://www.mathworks.com'
The search pattern '\w*://[^/]*' says look for a string that starts with some "word" characters ('\w*) corresponding to the protocol (e.g. http, https, rtsp), followed by the ubiquitous ://, and then any number of characters that are not a forward slash ([^/]*).
Edit: The 'once' option should eliminate a nested cell.
UPDATE: just the hostname, allowing inputs with no protocol.
>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
'https://www.mathworks.com/help/matlab/ref/strcmpi#dfvfv.html';
'google.com/voice'}
>> out = regexp(str,'([^/]*)(?=/[^/])','match','once')
out =
'www.mathworks.com'
'www.mathworks.com'
'google.com'
UPDATE 2: regexp madness!
>> str = {'http://www.mathworks.com/help/images/removing-noise-from-images.html';
'https://www.mathworks.com/help/matlab/ref/strcmpi#dfvfv.html';
'google.com/voice';
'http://monkey.org/';
'stackoverflow.com/';
'meta.stackoverflow.com'};
>> out = regexp(str,'.*?[^/](?=(/([^/]|$)|$))','match','once')
out =
'http://www.mathworks.com'
'https://www.mathworks.com'
'google.com'
'http://monkey.org'
'stackoverflow.com'
'meta.stackoverflow.com'
% hostname.m
function hostnames = hostname(str)
hostnames = regexp(str,'.*?[^/](?=(/([^/]|$)|$))','match','once');
Code:
function output_url = domain_name(input_url)
c1 = strfind(input_url,'//');
ind1 = strfind(input_url,'/');
if isempty(c1) && isempty(ind1)
output_url = input_url; % For case like - www.mathworks.com
return;
end
if ~isempty(c1)
if numel(ind1)>2
output_url = input_url(1:ind1(3)-1); % For cases like - http://www.mathworks.com/ or http://www.mathworks.com/something/
else
output_url = input_url; % For case like - http://www.mathworks.com
end
else
output_url = input_url(1:ind1(1)-1); % For cases like - www.mathworks.com/ or www.mathworks.com/something/
end
return;
Example runs:
%% Long URLs with extensions
disp(domain_name('www.mathworks.com/help/images/removing-noise-from-images.html'))
disp(domain_name('http://www.mathworks.com/help/images/removing-noise-from-images.html'))
%% Short URLs without HTTP://
disp(domain_name('www.mathworks.com'))
disp(domain_name('www.mathworks.com/'))
%% Short URLs with HTTP://
disp(domain_name('http://www.mathworks.com'))
disp(domain_name('http://www.mathworks.com/'))
Return:
www.mathworks.com
http://www.mathworks.com
www.mathworks.com
www.mathworks.com
http://www.mathworks.com
http://www.mathworks.com
An alternative method and probably efficient one would be to use REGEXP, but apparently I prefer numbers.
Edit 1: If you prefer to use bunch of URLs at the sametime, you may use a cell array. Obviously, the output would be a cell array too. Look at the following MATLAB script to get a feel of it -
% Input
in_urls_cell = [{'http://mathworks.com/'},{'mathworks.com/help/matlab/ref/strcmpi.html'},{'mathworks.com/help/matlab/ref/strcmpi#dfvfv.html'}];
% Get domain name
out_urls_cell = cell(size(in_urls_cell));
for count = 1:numel(in_urls_cell)
out_urls_cell(count)={domain_name(cell2mat(in_urls_cell(count)))};
end
% Display only domain name
for count = 1:numel(out_urls_cell)
disp(cell2mat(out_urls_cell(count)));
end
The above script returns -
http://mathworks.com
mathworks.com
mathworks.com

Resources