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

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: How to get a value from a file and save in another file
by ikegami (Patriarch) on Apr 27, 2005 at 16:13 UTC

    How about

    print("LA-1-00 LA-2-01 LA-03-2\n");

    Sorry, but I don't see the pattern between your input and output. Did you mean to print out all those starting with LA-? If so, you missed LA-03-02. The following prints all the tokens starting with LA-

    open(FILE, "< $filename") or die("Can't open input file: $!\n"); my $text = do { local $/; <FILE> }; close(FILE); print(join(' ', grep { /^LA-/ } split(/\s+/, $text)), "\n");

    Update: Maybe LA-03-2 and LA-03-02 are considered duplicates, and you're trying to remove duplicates? If so, the following code should help:

    open(FILE, "< $filename") or die("Can't open input file: $!\n"); my $text = do { local $/; <FILE> }; close(FILE); my %seen; print(join(' ', grep { if (/LA-(\d+)-(\d+)/) { my $key = "LA-" . ($1+0) . "-" . ($2+0); $seen{$key}++ ? 0 : 1; } else { 0 } } split(/\s+/, $text)), "\n");
      Thank you for the help. I want to print out the first value that start with "LA-" for each record. In my example file there are 3 records and each record has 1 or 2 or more values. In my real file there Hundreds records.
        use strict; use warnings; my $found_match = 0; while (<DATA>) { s/\s+$//; if (/^\d/) { $found_match = 0; } if (!$found_match && /^\d*\s+(LA.*)/) { print("$1\n"); $found_match = 1; } } __DATA__ 1 LA-1-00 2 CON-1-0 LA-2-01 3 LA-03-2 LA-03-02
Re: How to get a value from a file and save in another file
by bofh_of_oz (Hermit) on Apr 27, 2005 at 16:11 UTC
    Try a little more detail. It is quite hard to follow this kind of explanation. Do you need to sort text inside a file? Or sort output of file names? or something else altogether?

    --------------------------------
    An idea is not responsible for the people who believe in it...

Re: How to get a value from a file and save in another file
by holli (Abbot) on Apr 27, 2005 at 16:14 UTC
    You should use a regex.
    perl -n -e "@_ = $_ =~ m/(LA-\d+-\d+)/g; print qq(@_\n)" file


    holli, /regexed monk/
Re: How to get a value from a file and save in another file
by sir.shz (Novice) on Apr 27, 2005 at 18:21 UTC
    Still not clear what you mean by "first record", is "LA-03-2" and "LA-03-2" considered the same because the middle number "03" being the same, or the two numbers are numerically the same? Assuming the first:
    use strict; my %seen; while(<DATA>){ chomp; my @parts = split /-/; print $_,"\n" if $parts[0] eq 'LA' && !$seen{$parts[0]-$parts[1]}+ ++; } __DATA__ LA-1-00 CON-1-0 LA-2-01 LA-03-2 LA-03-02
      I am sorry for the miss leading the data I used. Let me try it again:
      The data file is:
      Record1: CON-aaa
      -----------LA-bbbbb
      -----------LA-aaaaa
      Record2: LA-12345
      -----------LA-1ad1d
      ...
      I want to output the first data with "LA" in record 1, record2 and so on. The result should be like:
      LA-bbbbb
      LA-12345
      There is no need to check if the rest of the value after the "LA-".

      Thank you again!!
Re: How to get a value from a file and save in another file
by Anonymous Monk on Apr 27, 2005 at 16:15 UTC
      Sorry for the cofusions and thanks for the help. I am new here I was not sure how to use the format yet. I am tring to output the first value with "LA" of each record.
      The data file should be:
      Record1: LA-1-00
      Record2: CON-1-0
      ------------LA-2-01
      Record3: LA-03-2
      ------------LA-03-02

      I need the output like:
      LA-1-00
      LA-2-01
      LA-03-2

        perl -nl -e '/^Record/../LA/ && /(LA.+)/ && print $1' datafile ?

        I'm sure I'm doing much more work than necessary - but it's hot in the office.