http://www.perlmonks.org?node_id=314986
Description: It's so often useful to quickly look through X12 files to analyze segment demographics and that sort of thing. However, since it isn't a line-based encoding, but rather a segment-based encoding... many typical line-oriented tools (grep, sort, uniq, perl -pne, etc.) can't be used. To be even more of a pain... X12 files don't even have standard delimitters. So here's a perl one-liner that spools an X12 file (or any number of similarly delimitted files) as though the segment separator were "\n".
perl -lpe 'BEGIN{$SIG{PIPE}="exit"; $|=1; $/=\108; ($/) = (<> =~ /^.{1
+05}(.?\r?\n?)/); seek ARGV, 0, 0}'
Typical uses might include
perl -lpe 'BEGIN{$SIG{PIPE}="exit"; $|=1; $/=\108; ($/) = (<> =~ /^.{1
+05}(.?\r?\n?)/); seek ARGV, 0, 0}' | grep ^NM1
Or, since this neat stuff sets $/ and is performed at begin time... it's the same as
perl -lne 'BEGIN{$SIG{PIPE}="exit"; $|=1; $/=\108; ($/) = (<> =~ /^.{1
+05}(.?\r?\n?)/); seek ARGV, 0, 0} print if /^NM1/'
In fact you can merge an arbitrary perl -ne or -pe in there.