Re: How to rearrange text fields in sed?
- From: Stephane CHAZELAS <this.address@xxxxxxxxxx>
- Date: Tue, 17 Jan 2006 21:01:55 +0000
2006-01-17, 14:29(-06), Ed Morton:
[...]
>>>>>>>awk 'ORS=(NR%4?" ":"\n")'
[...]
> I disagree. It's more like if in C I had written something concise like:
>
> done = (argc > 3 ? 1 : 0);
>
> instead of:
>
> if (argc > 3)
> {
> done = 1;
> }
> else
> {
> done = 0;
> }
>
> Yes, to anyone who doesn't know C the first form is slightly less
> readable BUT it's perfectly readable to anyone who does know the
> language and is a perfectly reasonable way to write the code.
[...]
It's not. In the C code above, you're using the ternary operator
for what it is meant to be used for which is perfectly fine even
if less readable than the longer form. You'll see that many
company have "general coding guidelines" that prohibit the use
of that ternary operator, though.
In
awk 'ORS=(NR%4?" ":"\n")'
Though it's a nice "trick", it is not good coding practice,
because:
- it puts the action in the condition part (the original awk
wouldn't even have allowed you to do that).
- it relies on a not straightforward side effect: the fact that
both " " and "\n" resolve to true. Actually, some of your
past posts that had that same kind of construct suffered from
bugs where the expression didn't resolve to true in corner
cases.
- the action is not clearly visible. It's the implicit {print},
but at first sight it looks like it's an assignment.
- it's misleading. As in my C code above, using "=" in a
condition may mislead one that could take it for a "=="
- it doesn't tell in "awk code" what you had in mind, or at
least what the algorithm is. The algorithm is: print the
current record, if the line number is a multiple of 4 append a
newline, append " " otherwise. Instead, your code translates
into "print the current record (implied with the current ORS
appended) only if the assignment to the ORS special variable
of a string that is "\n" if NR is a multiple of 4 or " "
otherwise resolves to true".
- it's unnecessarily compact.
I wouldn't have complained on:
awk '
{
ORS = (NR % 4 == 0 ? "\n" : " ")
}'
or if you hadn't made a point on its /maintainability/.
--
Stéphane
.
- Follow-Ups:
- Re: How to rearrange text fields in sed?
- From: Ed Morton
- Re: How to rearrange text fields in sed?
- References:
- How to rearrange text fields in sed?
- From: Florian K.
- Re: How to rearrange text fields in sed?
- From: Stephane Chazelas
- Re: How to rearrange text fields in sed?
- From: Ed Morton
- Re: How to rearrange text fields in sed?
- From: Stephane Chazelas
- Re: How to rearrange text fields in sed?
- From: Ed Morton
- Re: How to rearrange text fields in sed?
- From: Chris F.A. Johnson
- Re: How to rearrange text fields in sed?
- From: Ed Morton
- Re: How to rearrange text fields in sed?
- From: Stephane CHAZELAS
- Re: How to rearrange text fields in sed?
- From: Ed Morton
- How to rearrange text fields in sed?
- Prev by Date: Re: How to rearrange text fields in sed?
- Next by Date: Re: How to rearrange text fields in sed?
- Previous by thread: Re: How to rearrange text fields in sed?
- Next by thread: Re: How to rearrange text fields in sed?
- Index(es):
Relevant Pages
|
Loading