2

I have a number of files in a common directory (/home/test) with a common name:

ABC_1_20110508.out    
ABC_1_20110509.out
ABC_1_20110510.out
..

Each text file has one record that looks like this:

(count, 553076)

I would like to strip out the numbers and just list them out in a file one at a time.

553076
1005
7778000
...

Can someone show me how to do this using perl?

1
  • This is an interesting question because it mixes two concepts: 1. how to process many small text files. 2. how to extract the number values from there. Commented Jun 3, 2011 at 13:47

3 Answers 3

3

use this regex:

/\(\w+, (\d+)\)/

you can also use the magic diamond operator to iterate over all of the files at once:

while (<>) {
    # extract the number
    /\(\w+, (\d+)\)/;

    # print it out
    print $1, "\n";

}

And if your perl script is called myscript.pl, the you can call it like this:

$ myscript.pl /home/test/ABC_1_*.out
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting a can't open, no such file... Somehow it doesn't like that /home/test/ABC_1_*.out when I pass it in as you suggested. I can certainly ls -l ABC_1_* without no problems
@jdamae: maybe the full path is wrong? try cding to that directory and using myscript.pl *
Should the RE be "/(\w+, (\d+))/" or "/(count, (\d+))/"? There might be other lines that will match that regular expression like maybe "(average, 232)".
2

Sounds like a one-liner to me:

$ perl -wne '/(\d+)/ && print "$1\n"' *.out > out.txt

2 Comments

Thanks! Neat stuff. since I'm streaming out values (numbers). I'm interested in doing a sum. Can we do that too?
@jdamae You can do it either from the out.txt file, or the one-liner above. perl -wne '/(\d+)/ && ($i += $1); print qq($i\n);' *.txt Or if you prefer just to receive the sum from out.txt: perl -we 'while(<ARGV>) { $i += $_; } print $i' out.txt
0

Easiest way is to use the <> operator. When invoking a perl program without arguments, <> acts just like <STDIN>. If you call it arguments, <> will give you the contents of every file in @ARGV without you having to manually manage the filehandles.

Ex: ./your_script.pl /home/test/ABC_1_????????.out or cat /home/test/ABC_1_????????.out | ./your_script.pl. These would have the same effect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.