in reply to tricky text file
When tackling problems like this, it might also be useful to surf through a few tutorials on the awk tool, which is readily available in any Unix/Linux system. (It is a venerable old tool, and, in fact, is one of the original inspirations for Perl.) Awk is a much simpler yet more-targeted tool, designed specifically for this purpose, and its general approach is both informative and somewhat easier to see.
In short, an awk program consists of a series of regular-expression patterns (as well as BEGIN and END (-of-file ...) patterns), accompanied by blocks of code that are to be executed when each pattern is matched. Generally, a programmer uses the patterns to gather information from one-or-more lines, and then to recognize when a complete output-record has been accumulated and should be produced. It is not a general-purpose programming language, as Perl is, but it does very-clearly illustrate a powerful approach that is well-proved and widely applicable to situations like this. It also corresponds quite-directly to a corresponding Perl solution: by design and inspiration, Perl programs can be written in a very awk-like manner.
There are, as it turns out, a very lot of pragmatic situations, very much like this one, where Awk, or an implementation in another language of the strategy that it champions, is “just what the doctor ordered.” You will find yourself using it very frequently.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: tricky text file
by raidermike (Initiate) on Sep 10, 2015 at 20:04 UTC |