search and replace a string after a match - linux

I have a file that contains :
String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"
I try to change the string change_variable with something else like
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"
But when I use :
sed 's/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name=florian-testpeering-5"/'g
I Obtain :
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"=change_url"
What I did wrong ?
Edit :
To be more precise, I need to change the change_variable inside with a $peering who I declare before in my script.

The fact that you have forward slashes in the url, means that is better to use another character for the sed separator and so:
sed 's#String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"#String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"#g'
The normal / has been changed for #, allowing for easier reading and processing of the url.

Is this what you're trying to do?
awk 'index($0,"https://url_address/v2.0/vpc/peerings?name=change_variable") { sub(/change_variable/,"florian-testpeering-5") } 1' file
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"

If I understand your edit, and you are saying you have your string in a variable at the beginning of your script, similar to:
var='String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"'
and you need to change the contents of the string replacing from name=... to the end with a new value, you can simply use bash parameter expansion with substring replacement, e.g.
var="${var/name=*/name=florian-testpeering-5\"}"
Now the variable var will contain:
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"
You can do the same thing if the string you want to replace with is also stored in another variable, such as repl="lorian-testpeering-5", in that case your substring replacement would be:
var="${var/name=*/name=${repl}\"}"
(same result in var)

Related

nodejs how to replace ; with ',' to make an sql query

I have a query that looks like this:
INSERT INTO table VALUES ('47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3')
that is produced by parsing a csv file.
The query is not in a valid form, I have to replace all semicolons with the string ',' (comma inside single quotes). What I want to get is:
('47677','2019','2019','10T-1001-10010AS','A05','International','TieLineKoman-KosovoB','L_KOM-KOSB','2018','NULL','NULL','','NULL','Tieline','NULL','10XAL-KESH-----J','0','3')
I have tried to do this in many different ways, but I end up with backshlashes added in my string. This is what I get:
"INSERT INTO AllocatedEICDetail VALUES ('47677\\',\\'2019\\',\\'2019\\',\\'10T-1001-10010AS\\',\\'A05\\',\\'International\\',\\'TieLineKoman-KosovoB\\',\\'L_KOM-KOSB\\',\\'2018\\',\\'NULL\\',\\'NULL\\',\\'\\',\\'NULL\\',\\'Tieline\\',\\'NULL\\',\\'10XAL-KESH-----J\\',\\'0\\',\\'3')"
Any ideas how to do this properly without having the backslashes added?
Thank you!
//the string you have
const string = '47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3';
//the string you need:
const targetString = string.replace(/\;/g,',');
You specify a small regex between the forward slashes in replace which is a simple ';', give it a 'g' flag for global which will replace all instances, and in the second argument supply what you need it replaced with.

Go how can i efficient split string into parts

i am fighting with a string splitting. I want to split string by wildcards into a slice, but this slice should contain this wildcards as well.
For example: /applications/{name}/tokens/{name} should be split into [/applications/ {name} /tokens/ {name}] etc.
Here is a sample code i wrote, but it is not working correctly, and i don't feel good about it either.
https://play.golang.org/p/VMOsJeaI4l
There are some example routes to be tested. Method splitPath split path into parts and display both: before and after.
Here is a solution:
var validPathRe = regexp.MustCompile("^(/{[[:alpha:]]+}|/[-_.[:alnum:]]+)+$")
var splitPathRe = regexp.MustCompile("({[[:alpha:]]+}|[-_.[:alnum:]]+)")
func splitPath(path string) parts{
var retPaths parts
if !validPathRe.MatchString(path) {
return retPaths
}
retPaths = splitPathRe.FindAllString(path, -1)
return retPaths
}
I made this by creating two regular expressions, one to check if the path was valid or not, the other to extract the various parts of the path and return them. If the path is not valid it will return an empty list. The return of this will look like this:
splitPath("/path/{to}/your/file.txt")
["path" "{to}" "your" "file.txt"]
This doesn't include the '/' character because you already know that all strings in the return but the last string is a directory and the last string is the file name. Because of the validity check you can assume this.

regular expression with matching empty string

I am working on a django project to modify database options in the file settings.py.I want to use regular expression to do it.
the options just like :
'PASSWORD':'123456',
so I have write a function,the code is following:
def config_item(self,data,item,value):
rStr = "'"+item+"':(\s)?'\w*'"
src = "'"+item+"': '"+value+"'"
res = re.sub(rStr,src,data)
return res
So I can call like this to modify password to '000000',
data = config_item(data,'PASSWORD','0000')
But when the source password is blank or dest password is blank ,it does not work.That is ,it does not match 'PASSWORD':'',
Are there some wrong with the regular expression.
How do I write it rightly.
Maybe try using '[^']*' instead of '\w*'
I think \w is a bit more strict.

Is there a way to add quotes to a multi paragraph string

I wrote the following line:
string QuoteTest2 = "Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
This example went well, but what can I do in case I want to write a multi paragraph string including quotes?
The following example shows that puting '#' before the doesn't cut it..
string QuoteTest2 = #"Benjamin Netnayahu,\"BB\", said that: \"Israel will not fall\"";
The string ends and the second quote and the over just gives me errors, what should I do?
Use double quotes to escape ""
e.g.
string QuoteTest2 = #"Benjamin Netnayahu,""BB"", said that: ""Israel will not fall""";

Is there any way to retrieve a appended int value to a String in javaScript?

I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number

Resources