Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to add a new line after three or four lines from a pattern after getting that pattern

by thanos1983 (Parson)
on Apr 03, 2019 at 11:00 UTC ( #1232079=note: print w/replies, xml ) Need Help??


in reply to How to add a new line after three or four lines from a pattern after getting that pattern

Hello anirbanphys,

One possible way is:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use feature 'say'; my @lines = io('file.txt')->chomp->slurp; print Dumper \@lines; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # Insert at position 12, replace 0 elements. splice @lines, $i + 5, 0, 'default_operating_conditions : "AB0.5v4 +5c" ;'; last; # break loop } next; } print Dumper \@lines; __END__ $ perl test.pl $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ]; $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', 'default_operating_conditions : "AB0.5v45c" ;', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ];

Update: I have hard-coded the data but you can do the parsing. My approach is to read the file in an array so you can know the exact position of the line that you want to alter. Next step insert a new line without altering the array using splice.

Sample of code to split string and insert it:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use feature 'say'; my @lines = io('file.txt')->chomp->slurp; print Dumper \@lines; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, "default_".$elements[1]." : ".$elements[2].""; last; # break loop } next; } print Dumper \@lines; __END__ $ perl test.pl $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ]; $VAR1 = [ 'library(and_gate) {', ' delay_model : table_lookup ;', ' date : "Fri Mar 15 03:44:39 " ;', ' time_unit : 1ms ;', ' voltage_unit : 1V ;', ' current_unit : 1A ;', '', ' operating_conditions ("AB0.5v45c") {', ' process : 1 ;', ' temperature : 45 ;', ' voltage : 0.5 ;', ' }', 'default_operating_conditions : "AB0.5v45c" ;', '', ' input_voltage(default) {', ' vi : 0 ;', ' vh : 0.5 ;', ' vim : 0 ;', ' vin : 0.5 ;', ' }', '', '}' ];

Update 2: All the commends together in one solution:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('file.txt')->chomp->slurp; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, " default_".$elements[1]." : ".$elements[2].""; last; # break loop } next; } # print Dumper \@lines; io('out.txt')->appendln($_) for @lines; __END__ $ perl test.pl $ cat out.txt library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } default_operating_conditions : "AB0.5v45c" input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }

Update 3: Adding a better (more strict) regex to the solution:

#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; my @lines = io('file.txt')->chomp->slurp; for my $i (0 .. $#lines) { if ($lines[$i] =~ m/\s\soperating_conditions*/) { # split line based on white space my @elements = split /\s+/, $lines[$i]; # remove first and last character of the string $elements[2] =~ s/^.(.*).$/$1/; # Insert string at position 12, replace 0 elements. splice @lines, $i + 5, 0, " default_".$elements[1]." : ".$elements[2]." ;"; last; # break loop } next; } # print Dumper \@lines; io('out.txt')->appendln($_) for @lines; __END__ $ perl test.pl $ cat out.txt library(and_gate) { delay_model : table_lookup ; date : "Fri Mar 15 03:44:39 " ; time_unit : 1ms ; voltage_unit : 1V ; current_unit : 1A ; operating_conditions ("AB0.5v45c") { process : 1 ; temperature : 45 ; voltage : 0.5 ; } default_operating_conditions : "AB0.5v45c" ; input_voltage(default) { vi : 0 ; vh : 0.5 ; vim : 0 ; vin : 0.5 ; } }

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!
  • Comment on Re: How to add a new line after three or four lines from a pattern after getting that pattern
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to add a new line after three or four lines from a pattern after getting that pattern
by anirbanphys (Beadle) on Apr 03, 2019 at 11:35 UTC
    Thank you thanos1983 for your help.

    The above mentioned code will be working if we remove the "HARD CODED" "default_operating_conditions : "AB0.5v45c" ;". If you look at my code, I am doing some operations for getting "AB0.5v45c" from " operating_conditions ("AB0.5v45c") { " . Can you please review the code

    #!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; use feature 'say'; my @operating_cond; my @lines = io('file.txt')->chomp->slurp; #print Dumper \@lines; for my $i (0 .. $#lines) { if ($lines[$i] =~ /operating_conditions/) { #print "$lines[$i]\n"; @operating_cond=split (operating_conditions,$lines[$i]); @operating_cond=split ("{",$operating_cond[1]); @operating_cond=split (/\(/,$operating_cond[0]); @operating_cond=split (/\)/,$operating_cond[1]); # Insert at position 12, replace 0 elements. splice @lines, $i + 5, 0, 'default_operating_conditions : "@operat +ing_cond" ;'; last; # break loop } next; } print Dumper \@lines;

    Thank you for your help :)

      Hello again anirbanphys,

      See the updated version that contains solution to the hard-coded problem. Now the script will pick up the string withing the interesting line.

      Hope this helps. Let me know if you can not understand what the code does.

      BR / Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        SURE !!! Yes I just got the code :D. And it is working. I commented the unwanted section. One Last help, how to get rid of from extra characters from the output? Do I have to parse again to delete the extra characters?

Re^2: How to add a new line after three or four lines from a pattern after getting that pattern
by anirbanphys (Beadle) on Apr 03, 2019 at 11:44 UTC
    Thank you thanos1983 for your help.

    The above mentioned code will be working if we remove the "HARD CODED" "default_operating_conditions : "AB0.5v45c" ;". If you look at my code, I am doing some operations for getting "AB0.5v45c" from " operating_conditions ("AB0.5v45c") { " . Like,

    splice @lines, $i + 5, 0, 'default_operating_conditions : "$operating_ +cond[0]" ;';
    One more thing, how to avoid to print the very first "$VAR1 = [" section?

    Thank you for your help :)

      Hello again anirbanphys,

      Regarding the hard-coded part I have already replied to your question :). Regarding the first print simply commend out the line: # print Dumper \@lines;.

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        Hello thanos1983 ,

        It is great to see that continuously you are helping me to improve the skill in perl coding. Yes I added your suggested code in the actual code.

        Thank you for your help :)

        Thanks you and best regards

        Anirban

        Hello thanos1983,

        I am able to print the desired output in a file. Thank you for your help and give me your valuable time to resolve the issue :). Take care :)

        With Best Regards Anirban
        Hi thanos1983,

        I am very sorry, you have given me the solution but still I am not able to get the desired result. You are correct, I meant single quotes, $var1 as extra characters in the output. But let me check the cpan documentation for the correct way to print it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1232079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2020-05-30 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?