in reply to Extract data from txt file
here a oneliner version (that skips tuple without range specification). Pay attention to windows double quotes
perl -nle "$x++ if 0==$.%4;$a[$x].=$_}{for(@a){next unless/Range/;s/ST +/ST,/;/\s(\d+)(\w) - \d+(\w)\W/;$n=$1;for $l(qq($2)..qq($3)){print s/ +\(.*\)/$n$l, /r}}" add-data.txt 432 10TH ST, APT 2A, BROOKLYN NY 10598-6601 432 10TH ST, APT 2B, BROOKLYN NY 10598-6601 432 10TH ST, APT 3A, BROOKLYN NY 10598-6601 432 10TH ST, APT 3B, BROOKLYN NY 10598-6601 432 10TH ST, APT 4A, BROOKLYN NY 10598-6605 432 10TH ST, APT 4B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5A, BROOKLYN NY 10598-6605 432 10TH ST, APT 5B, BROOKLYN NY 10598-6605 432 10TH ST, APT 5C, BROOKLYN NY 10598-6605 432 10TH ST, APT 5D, BROOKLYN NY 10598-6605
The trick is to read 3 lines at time (2 interesting plus the empty one in the middle) to build a longer line. Then with capturing parens you can grab the range to expand. See it deparsed:
perl -MO=Deparse -nle "$x++ if 0==$.%4;$a[$x].=$_}{for(@a){next unless +/Range/;s/ST/ST,/;/\s(\d+)(\w) - \d+(\w)\W/;$n=$1;for $l(qq($2)..qq($ +3)){print s/\(.*\)/$n$l, /r}}" add-data.txt BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; ++$x if 0 == $. % 4; $a[$x] .= $_; } { foreach $_ (@a) { next unless /Range/; s/ST/ST,/; /\s(\d+)(\w) - \d+(\w)\W/; $n = $1; foreach $l ("$2" .. "$3") { print s/\(.*\)/$n$l, /r; } } } -e syntax OK
L*
|
|---|