On 23.08.2015 18:04, Kenny McCormack wrote:
> One of the historical weaknesses of AWK has been that of maintaining the
> appearance of tabular data - such as the output of the "ps" command.
> Generally, people don't even bother to try to continue to use the built-in
> AWK capabilities once they see how AWK can mess things up, so they end up
> using various workarounds, such as using printf() to reformat everything.
>
> But let's assume that we want to make it work with the built-in facilities.
>
> Assume that we have a (Unix/Linux) command line something like:
>
> $ ps ... | gawk 'BEGIN {OFS="\t"} {$3=$4=$5=$6="";$9="something";print}'
>
> The intent is to delete several of the fields and to change the value of
> another field, and then to print it out with tabs (i.e., a single tab)
> between each of the printed fields. Unfortunately, what we get is output
> with runs of multiple tabs because there really is no way to actually
> *delete* a field; you can only set it to empty. So, my question is:
> Shouldn't this work? Shouldn't there be a way to make it work (other than
> the obvious workarounds) ?
It would be very useful if awk would be able to simply support that.
To me it seems it would need to remove the field and the subsequent or
preceding field separators (inconsistencies at begin and end of record).
In the general case, though, I can't think of a way to correctly remove
a field; for example (assuming standard FS):
AAA Hello 12345
BBB Hi 42
CCC H 1
how would one remove the second field without spoiling the format? (And
it may become much worse if blanks and TABs are mixed.)
For practical purposes - depending on the used system - I'd probably use
in many cases just some external means, like
ps | awk '...' | column -t
(but note that this is also not bullet-prove, say in case of ps -f as
source of input). But note that the problem is the inhomogeneous data
source; with a clean (well defined) parsable syntax you could do more,
but most sources (including [historic] output of Unix or DOS commands)
are not satisfying in that respect.
That all said, and answering your question; I don't think there could
be a [simple] way to solve the general task.
Janis
>
> [...]