Linux Replace a particular string with consecutive numbers [closed] - regex

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 3 years ago.
Improve this question
CONFIG.CONFIG
create state file${number_of_edges} {
{interval=60
idle_interval_notification="DISABLED"}
}
create state file${number_of_edges} {
{interval=60
idle_interval_notification="DISABLED"}
}
I have file which has content like above. I want to replace ${number_of_edges} string with consecutive numbers like 0,1,2 and so on
EXPECTED OUTPUT
create state file0 {
{interval=60
idle_interval_notification="DISABLED"}
}
create state file1{
{interval=60
idle_interval_notification="DISABLED"}
}
create state file2 {
{interval=60
idle_interval_notification="DISABLED"}
}

I got the solution.
Command -
awk -vRS=edge '{$0=n$0;ORS=RT}++n' FILE

A simple awk script:
awk 'sub("\\${number_of_edges}",cnt+1, $0){cnt++}1' input.txt
Explanation:
sub("\\${number_of_edges}",cnt+1, $0) For each input line, substitute, ${number_of_edges} with variable (cnt + 1)
{cnt++} If substituted more than 0, increment variable cnt
1 output each input line

Related

Insert a quote in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 months ago.
Improve this question
I'm looking to insert a quote in a string, but keep everything else. So, an example string:
' "2020-10-10",8000,"Hello" '
I want to put quotes around 8000 (or whatever number is there). so:
' "2020-10-10","8000","Hello" '
How would I do that in regex?
I'm not an expert on regex but you can do it you just have to do it twice. Because I couldn't figure out a way to look for ",char or char,".
function test() {
try {
let a = ' "2020-10-10",8000,"Hello" ';
a = a.replace(/,/g,'","');
a = a.replace(/""/g,'"');
console.log(a);
}
catch(err) {
console.log(err);
}
}
7:26:23 AM Notice Execution started
7:26:23 AM Info "2020-10-10","8000","Hello"
7:26:23 AM Notice Execution completed

Update series of numeric values in long string [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 2 years ago.
Improve this question
I have text column with following examplary data:
5,5,0.1;6,6,0.15;7,7,0.2;8,8,0.25;9,9,0.3;10,10,0.35;11,11,0.4;12,12,0.45;13,13,0.5;14,14,0.55;15,15,0.6;16,16,0.65;17,17,0.7;18,18,0.75;19,19,0.8;20,20,0.85;
I need to add some fixed value to each of numeric values (the one before semicolon)
so for example from:
5,5,0.1;6,6,0.15; I want add 0.15 so result would be:
5,5,0.25;6,6,0.3;
I guess I should try something with regexp_replace but I have no idea how to start here
The correct solution would be fix your broken data model and not store multiple, delimited values in a single column.
I wouldn't do this with a regex, but unnesting the elements of the string, adding the value to the third element, then aggregate everything back into the broken design:
update badly_designed_table
set denormalized_column =
(select string_agg(concat_ws(',', a, b, round(c + 0.15,2)), ';' order by idx)
from (
select split_part(val, ',', 1) as a,
split_part(val, ',', 2) as b,
split_part(val, ',', 3)::numeric as c,
idx
from unnest(string_to_array(bad_column, ';')) with ordinality as x(val,idx)
-- skip the "empty" element generated by the trailing ;
where nullif(val, '') is not null
) t)

How to use regex for a persons name? [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 7 years ago.
Improve this question
Basically here is my code and I cannot find the problem with this, So I'm looking for advice.
name = raw_input('What\'s your name? ')
if not re.match(r'[A-Za-z- ]+', name):
print 'Invalid name.\n' #error message
You must need to add end of the line anchor. Without the anchor foo? would be considered as a valid one. That is, it won't print the message Invalid name for this name.
if not re.match(r'[A-Za-z- ]+$', name):
print 'Invalid name.\n'
Example:
>>> s = 'foo?'
>>> if not re.match(r'[A-Za-z- ]+', s):
print('Invalid name.\n')
>>> if not re.match(r'[A-Za-z- ]+$', s):
print('Invalid name.\n')
Invalid name.

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);