I have a series of tab-separated files which look like this:
1 7083
1 7530
1 ----------
1 -7840
3 0
20 00000
4 00001
1 00007
3 00010
1 00011
1 00023
2 00044
3 00100
1 00101
1 00112
1 00120
1 00121
1 00149
1 00186
1 00193
1 00200
2 00202
4 00683
3 00725
3 00727
1 00731
1 00735
1 00738
1 00745
1 00749
1 00761
1 00777
2 00778
1 00784
3 00801
12 00802
3 00803
1 00820
4 00823
Because the second field represents a zip code, I want to kill any lines in a file which do not conform to zip code requirements. However, my perl one-liner attempts are failing. How can I get this one-liner to change the file so that only lines which match my filter stay? Here is my attempt:
[tbone@MDB zip-grok]$ perl -pi.bak -e '($count, $zip) = split /\t/; ch
+omp $zip; warn "*$zip*"; next unless ( ($zip =~ /\d{5}/) and ($zip >
+713) and ($zip < 99930) )' *.dat
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
edited by ybiC: balanced <readmore> tags
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.