richz has asked for the wisdom of the Perl Monks concerning the following question:

So i would like to do some in-place editing of certain files and want some suggestions. Basically I am looking for portions of the file with the following:
Tag { Description "blah blah blah" ... Name "test tag" ... }
I need to remove whitespace from the name that is in between the quotes. What I am currently doing is the following:
$^I = ".bak"; @ARGV = "test.txt"; while(<>) { if(/^\s+Tag {/) { print; #skip over lines before 'Name' line while(<>) { if(/^\s+Name/) { s/"(.*)\s+(.*)/"$1$2"/g; print; last; } else { print; } } else { print; } }
There has to be a better way to do this..

Replies are listed 'Best First'.
Re: In-place editing of files
by Aristotle (Chancellor) on Sep 20, 2004 at 20:20 UTC

    Did you look at the documentation? There's no break in Perl. If you mean the break from C, that's called last.

    But you don't need that anyway. If your format does not allowing nesting other curly-delimited sections inside the Tag section, I'd do the following:

    $^I = ".bak"; @ARGV = "test.txt"; while(<>) { if( /^\s+Tag {/ .. /^\s+}/ ) { next unless /^(\s+Name[^"]*)"([^"]+)"/; my ( $directive, $value ) = ( $1, $2 ); $value = s/\s+//g; $_ = $directive . qq["$value"\n]; } print; }

    Makeshifts last the longest.

      Yeah it was a brainfart. I figured it out after I posted.
      A few things I think are wrong with your solution: The use of the next is incorrect because the 'Tag' line will be looked for again since we are going to the top of the while loop. You are missing a '~' on the $value line. Also, could you explain what that '..' is used for in that context in the if condition?

        Why did you conclude it's wrong to use next when you didn't know what .. does? As for what it does, that's in the documentation. I'll quote perlop (emphasis mine):

        In scalar context, ".." returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each ".." operator maintains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. It doesn't become false till the next time the range operator is evaluated. It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once. If you don't want it to test the right operand till the next evaluation, as in sed, just use three dots ("...") instead of two. In all other regards, "..." behaves just like ".." does.

        The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. The precedence is a little lower than || and &&. The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encountered. The final sequence number in a range has the string "E0" appended to it, which doesn't affect its numeric value, but gives you something to search for if you want to exclude the endpoint. You can exclude the beginning point by waiting for the sequence number to be greater than 1.

        In other words, that if will fail until it sees a ^\s+Tag {, at which point it will always succeed until a ^\s+} comes along. The outer loop reads the lines, and the flip-flop picks the ranges of interesting lines to which to apply the if block.

        Of course (and that's why I mentioned it), this will not properly respect nesting blocks (but they can easily be taken into consideration with an open-blocks counter).

        Makeshifts last the longest.

Re: In-place editing of files
by Zaxo (Archbishop) on Sep 20, 2004 at 20:26 UTC

    Is this anywhere close? $ perl -pi.bak -e'substr($_,index $_,q/"/) =~ s/\s+//g' file.txt Think of a better sed :-)

    After Compline,
    Zaxo

      The OP says

      I need to remove whitespace from the name that is in between the quotes.

      So it would have to be

      $ perl -pi.bak -e'substr($_,index $_,q/"/) =~ s/\s+//g if /^\s+Tag\s*{ +/ .. /^\s+}/ and /^\s+Name\b/' file.txt

      Makeshifts last the longest.

Re: In-place editing of files
by qumsieh (Scribe) on Sep 20, 2004 at 20:27 UTC
    Instead of break, you really need to have next. Also, you'd better check for the end of your block, a lone closing brace (according to your example), and quit the inner while() loop when you find it:
    last if /^\s*}\s*$/;
Re: In-place editing of files
by ww (Archbishop) on Sep 20, 2004 at 20:28 UTC
Re: In-place editing of files
by TedPride (Priest) on Sep 20, 2004 at 23:48 UTC
    $^I = ".bak"; @ARGV = "inplace.txt"; my $flag, my @arr; while(<>) { if (!$flag && /^\s*Tag {/) { $flag = 1; } elsif ($flag) { if (/^\s*}/) { $flag = 0; } elsif (/^\s*Name/) { (@arr) = split(/"/, $_, 3); @arr[1] =~ s/\s//g; $_ = join('"', @arr); $flag = 0; } } print; }
    This code at least works, even if it isn't as pretty as it could be.
Re: In-place editing of files
by seuratt (Novice) on Sep 21, 2004 at 05:32 UTC
    i'll throw this in the 'alternative' pile, since i'm not sure it qualifies as 'easier'.
    #!/usr/bin/perl -w { local $/ = undef; # slurp. $_ = <>; } $nb = qr/[^\}]*/; # non-close-bracket $nq = qr/[^\"]*/; # non-quote while( m/^Tag+ \s+ \{ $nb \n\s+ Name \s+ \" ($nq \s $nq) \" $nb \} /msx ) { substr( $_, $-[1], $+[1]-$-[1] ) =~ s/ /_/g; } print;
    i hadn't heard of the substr() lval return trick until this thread; it's pretty useful. also hadn't heard of @- or @+ till digging around. felt i'd share.

    re: earlier comment -- the $N vars are immutable, so you can't s/// them, sadly.

Re: In-place editing of files
by Spidy (Chaplain) on Sep 21, 2004 at 02:52 UTC
    Couldn't you use regex to find the stuff in quotes line by line, and then use $1 =~ s/ //; to get rid of all the spaces within the stuff in quotes?

    ...Keep in mind, this is mostly the suggestion of a relative n00b. AND untested.
Re: In-place editing of files
by Spidy (Chaplain) on Sep 21, 2004 at 13:55 UTC
    What if you stored the $N variable into another one before using s///?
      my first run at this problem actually landed me with something like that, but it lacked the grace of a good hack. deciding that a concise solution was not to be found with this problem, i chose to try and compensate with legibility.

      here's that awkward first solution of mine using the temp val:

      s/ ( ^Tag \s+ \{ [^\}]* \n \s+ Name \s+ \") (.*?) (\" [^\}]* \} ) /join '', $1, despace($2), $3 /emsgx; print; sub despace { my $x = shift; $x =~ s/ //g; return $x; }