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

Hi there
I'm sure there is an easy answer to this, I just cant think of it at present (probably due to the thumping headache that has appeared .. sigh).
Anyway ... I have a number of text files, the contents look like this.
[ [ 0.039543323] [ 0.068993981] [ 0.086558227] [ 0.12252527] [ 0.14724698] [ 0.16631099] [ 0.18877556] [ 0.20234162] [ 0.18110326] [ 0.13155017] etc ... until [ -4.0316574] [ -3.6178487] [ -3.1186917] ]
Could anyone please suggest a way of removing the sqaure brackets? I was considering grep but cant think beyond that ... lol.

Replies are listed 'Best First'.
Re: removing square brackets
by borisz (Canon) on Oct 20, 2005 at 13:40 UTC
    perl -i -pe 'y/[]//d' yourfile
    Boris
      This gets my vote. I have a rule, "never use s/// when a tr/// (or y/// in this case) will do." I ran some benchmark tests a while ago on s vs tr and tr is much faster at deleting characters from a string. (This might be stating the obvious.)
        I agree with kwaping, I would use tr/// for this solution.
        #!/usr/bin/perl -w use strict; while (<DATA>) { chomp; tr/[]//d; $_ = join(" ",split " ", $_); print "$_\n"; } __DATA__ [ [ 0.039543323] [ 0.068993981] [ 0.086558227] [ 0.12252527] [ 0.14724698] [ 0.16631099] [ 0.18877556] [ 0.20234162] [ 0.18110326] [ 0.13155017] [ -4.0316574] [ -3.6178487] [ -3.1186917] ]
Re: removing square brackets
by blazar (Canon) on Oct 20, 2005 at 13:33 UTC
    Do you mean the grep program or Perl's grep? Whatever, the answer is no. As I feel kind, the correct solution is (or {s,c}ould be)
    s/[\[\]]//g;
    Since this is only a hint I'll also tell you about
    perl -lpi -e '...'
    separately. It is left as an exercise to the reader to check up the documents or any introductory Perl book and put it all together.
Re: removing square brackets
by Fletch (Bishop) on Oct 20, 2005 at 13:31 UTC
    ruby -ne 'puts$1if/(-?\d(?:\.\d+)?)/' < data > munged

    Update: To everyone griping and downvoting because it's not a perl solution, if you just change five characters (not counting s/ruby/perl/) it is valid perl. Have to leave something for the people asking extremely trivial questions yet providing no code of their own to figure out on their own . . .

    (Hint: you need one more command line flag and change one keyword name)

    Update 2: Awww, someone spoiled the exercise for the reader.

      Translation to Perl:
      perl -lne 'print$1if/(-?\d(?:\.\d+)?)/' < data > munged

      Update: I posted this before Fletch updated his node.

Re: removing square brackets
by ides (Deacon) on Oct 20, 2005 at 13:33 UTC

    Sure you just need to use a regular expression. Open each file and loop over each line like this:

    while( my $line = <IN> ) { chomp($line); $line =~ s/\[//g; # Remove ['s $line =~ s/\]//g; # Remove ]'s # Either print out to STDOUT here or # write to another file }

    Frank Wiles <frank@wiles.org>
    http://www.wiles.org

Re: removing square brackets
by chester (Hermit) on Oct 20, 2005 at 13:35 UTC
    If this is all you're doing to the files, look at the -i, -e and -n/-p switches to perl in perldoc perlrun. Also perlre.
Re: removing square brackets
by svenXY (Deacon) on Oct 20, 2005 at 13:36 UTC
    Hi,
    perl -lpi.bak -e 's/[\[\]\s]//g' files*

    Regards,
    svenXY
Re: removing square brackets
by Angharad (Pilgrim) on Oct 21, 2005 at 09:41 UTC
    Thanks for the suggestions.
    And just to defend myself slightly .. yes I know it was a 'trivial question' and I did happen to mention that .. but when one is unwell ...
      As proof there is no such thing as a bad question, look at all the creative answers your "trivial question" sparked.

      Now, there is such a thing as a badly-worded question, but yours wasn't one of those. :)