Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Search and add lines into a file. Getting bareword error.

by freekngeek (Acolyte)
on Apr 19, 2013 at 07:54 UTC ( #1029475=perlquestion: print w/ replies, xml ) Need Help??
freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am working on a script. I need to search for specific string and add two lines below that string. Like this :

This is my file lvtnfet cmos20lpm lvtnfet_b symbol d g s b PROPMAP m=m And this would be my output file lvtnfet_float cmos20lpm lvtnfet_b symbol d g s b PROPMA +P m=m lvtnfet_auxpc2 cmos20lpm lvtnfet_b symbol d g s b PROPM +AP m=m
I am using this code just to test my script :
#!/tool/pandora/.package/perl-5.12.1/libexec/perl5.12.1 -w use strict; use warnings; my $file="device.map"; open (FH, "< $file") || die "Could not open file: $!\n"; my @lines = <FH>; close(FH); open (FH, "> $file"); for (@lines){ if ($_=~m/lvtnfet cmos20lpm lvtnfet_b symbol d g s b PR +OPMAP m=m/) print FH "lvtnfet_float cmos20lpm lvtnfet_b symbol d g +s b PROPMAP m=m \n"; print FH "lvtnfet_auxpc2 cmos20lpm lvtnfet_b symbol d g +s b PROPMAP m=m \n"; } print FH close FH; print "done\n";
I am getting so many errors like these
Bareword found where operator expected at device.map line 4, near "// +GLOBALFOUNDRIES" (Missing operator before GLOBALFOUNDRIES?) Bareword found where operator expected at device.map line 6, near "// +except" (Missing operator before except?) Bareword found where operator expected at device.map line 47, near "m= +m AREA=area" Bareword found where operator expected at device.map line 48, near "m= +m AREA=area"
I hope if someone could help me with that. Thanks.

Comment on Search and add lines into a file. Getting bareword error.
Select or Download Code
Re: Search and add lines into a file. Getting bareword error.
by hdb (Prior) on Apr 19, 2013 at 08:05 UTC

    The line

    print FH

    does not make sense. Did you mean

    print FH $_;

    UPDATE: A pair of {} is missing after the "if" line as well. print FH $; does not make sense either.

    Can you make the script work in your sense, and re-post to make sure I can look at the right one? It is a bit difficult to guess what the issue is...

    CORRECTION: It seems to create your desired output now...

Re: Search and add lines into a file. Getting bareword error.
by 2teez (Priest) on Apr 19, 2013 at 08:41 UTC

    Using the OP data provided, the following work for me:

    use warnings; use strict; while (<DATA>) { chomp; for my $to_add (qw[_float _auxpc2]) { ( my $string = $_ ) =~ s/^(.+?)(\s+?.+?)$/$1$to_add$2/; print $string, $/; } } __DATA__ lvtnfet cmos20lpm lvtnfet_b symbol d g s b PROPMAP m=m
    Output
    lvtnfet_float cmos20lpm lvtnfet_b symbol d g s b PROPMA +P m=m lvtnfet_auxpc2 cmos20lpm lvtnfet_b symbol d g s b PROPM +AP m=m
    UPDATE:
    "..I need to search for specific string and add two lines.."
    then you can do like so:
    while(<DATA>){ ... if (/^lvtnfet/) { ... else { print $_, $/; } }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Search and add lines into a file. Getting bareword error.
by bimleshsharma (Beadle) on Apr 19, 2013 at 10:33 UTC

    I guess this would fulfill your requirments hopefully...

    while(<FILE>) { if( $_ =~ /^lvtnfet/){ print FH $_; print FH NewLine; print FH NewLine; } else{ print FH $_; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029475]
Approved by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2014-12-20 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls