replace a string in config file using perl [closed] - regex

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a string having the following format:
ip = 192.168.1.2
user = test
password = test0
ip =, user = and password = are fixed but the strings to the right of the equals signs may vary.
Using Perl, I would like to write a one-line command to replace the string following user = with a given string, say, X, to return the following new string:
ip = 192.168.1.2
user = X
password = test0
Thanks for your help.

$ perl -p -e 's/^user =.*/user = X/' file
ip = 192.168.1.2
user = X
password = test0
If you want the string to substitute to be configurable, you can use the following:
$ user=X
$ perl -spe's/^user =\K.*/ $user/' -- -user="$user" file
ip = 192.168.1.2
user = X
password = test0
See Specifying file to process to Perl one-liner.

Related

Trying to find a way to list all friday dates between 2 dates in powershell [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How to identify a list of dates with a specific day of the week between 2 dates in Powershell
No problem, it's fairly straightforward:
$date = [datetime]::parseexact('07-Feb-20', 'dd-MMM-yy', $null)
$date2 = [datetime]::parseexact('28-Feb-20', 'dd-MMM-yy', $null)
$fridays = 1..($date2 - $date).Days | % {$date.AddDays($_)} | ? {$_.DayOfWeek -eq 'Friday'}
Just make sure you put your dates in correctly! There's likely a more succinct way to do it.
Every PowerShell [datetime] object contains a .DayOfWeek parameter which will tell you the day of the week. You can interate through the date items with another [datetime] feature, the .AddDays() method. So something like this:
$StartDate = [datetime]'datehere'
$EndDate = [datetime]'datehere'
$ThisDate = $StartDate
$AllFridays = #()
While ($ThisDate -le $EndDate)
{
If ($ThisDate.DayOfWeek -eq 'Friday') { $AllFridays += $ThisDate }
$ThisDate.AddDays(1)
}
This was just a first crack. Obviously, you could find the first friday, and then add days 7 at a time until you were past the End date, but I leave that optimization as an exercise for the reader. :)

Separate value after certain character by space in Swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
hi i have a value which is
Blockquote
1 1:0.0644343 1 1:0.0309334 1 1:0.0261616
Blockquote
i want to separate value by space but after certain character to get result like this..is there any possible solution . i know we can do in regex
Blockquote
"1 1:0.0644343"
"1 1:0.0309334"
"1 1:0.0261616"
Blockquote
I think regex is the perfect tool here:
var str = "Blockquote 1 1:0.0644343 1 1:0.0309334 1 1:0.0261616 Blockquote"
let regex = try! NSRegularExpression(pattern: "(\\d \\d:[\\.0-9]+)", options: [])
let matches = regex.matchesInString(str, options: [], range: NSMakeRange(0, str.characters.count))
for m in matches.reverse() {
let range = m.rangeAtIndex(1)
let startIndex = str.startIndex.advancedBy(range.location)
let endindex = startIndex.advancedBy(range.length)
let value = str[startIndex..<endindex]
str.replaceRange(startIndex..<endindex, with: "\"\(str[startIndex..<endindex])\"")
}
print(str)

R: Find product codes using Regular Expressions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I have a list of product item descriptions. I have loaded this into R. Most of these descriptions are utter nonsense and we are trying to extract a decent item code from them.
Instead of going through it line by line, can I use a regular expression in R to create a new vector that will only have integer values from the list?
I have most of the code now
JJ <- read.csv2(file.choose(),header= TRUE)
JJ$X <- gsub(pattern = "[0-9]+", replacement = "",
x = JJ$LGY_DHB_ITEM_DESCRIPTION, ignore.case = TRUE)
But I am unsure what to put in the replacement argument.
you can try replacing non (^) numerical ([:digit:]) characters with empty string :
gsub("[^[:digit:]]*", "", 'PRIVATE CONTRACT INV 710456354')
[1] "710456354"
but this wont work if you have more than one numeric in your string:
gsub("[^[:digit:]]*", "", 'PRIVATE 123 CONTRACT INV 710456354')
[1] "123710456354"
You could try to find the longest numercial in each string:
JJ <- data.frame(LGY_DHB_ITEM_DESCRIPTION=c('PRIVATE CONTRACT INV 710456354', 'PRIVATE 123 CONTRACT INV 710456354'))
m <- gregexpr("[0-9]*", JJ$LGY_DHB_ITEM_DESCRIPTION)
all_m <- regmatches(JJ$LGY_DHB_ITEM_DESCRIPTION, m)
JJ$X <- mapply(FUN =function(stri,idx) stri[idx],all_m, sapply(lapply(all_m,nchar),which.max))
JJ
LGY_DHB_ITEM_DESCRIPTION X
1 PRIVATE CONTRACT INV 710456354 710456354
2 PRIVATE 123 CONTRACT INV 710456354 710456354

process an input file with "name = value " pairs using perl [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The simulation code I use needs to read parameters from an input.txt file which looks like
paramA = 1,paramB = 2,
paramC = 3,paramD = 4,
When scanning a parameter (like paramC) in my simulation, I need to change the value of paramC every time manually. How can I do this with a perl script so that when I type
perl scriptname input.txt paramC 100
in the command line I can get a modified input file with paramC changed to 100
paramA = 1,paramB = 2,
paramC = 100,paramD = 4,
I can do this by creating a template file like
paramA = 1,paramB = 2,
paramC = <>,paramD = 4,
and then use perl to match the mark <> and replace it with the value I want. However is there a more direct way to match the parameter name and change its value ?
thanks.
The obvious answer is to use a regular expression. Perl is quite good at those.
So you could - for example - do:
s/paramC = \d+/paramC = $value/g;
Which'll do the trick I'd have thought?
Edit: Or use TLP's pattern in the comments:
s/^paramC = \K[^ ,]+/$value/g;
or perhaps:
s/^paramC = \K\d+/$value/g;

compare 2 arrays and get the values which are not matched :using perl map command or loops simply [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
arr1 = 1,2,3,4,5;
arr2 = 1,2,3;
want to compare and output as arr3=4,5;
Please help
thanks in advance
arry::utils error out, looks like some problem with the package, so that option is ruled out.
sub diff_array {
my ($a1, $a2) = #_;
my %h;
#h{#$a2} = ();
return grep !exists $h{$_}, #$a1;
}
my #arr1 = (1,2,3,4,5);
my #arr2 = (1,2,3);
my #arr3 = diff_array(\#arr1, \#arr2);