On 6/2/2012 6:26 PM, Harry wrote:
> Ed Morton wrote...
>
> [...]
>
>>>> On 6/1/2012 3:24 PM, Harry wrote:
>>>>> [...]
>>>>>>> awk -F, '/>>>>>/ {close(file); file=$1 ".txt"} {print> file}'
> [...]
>> You're welcome. If you're going to be using awk, though, I recommend you get
> the
>> book "Effective Awk Programming, 3rd Edition" by Arnold Robins. There's a
> couple
>> of things in the script you posted above ("if (..)" and "print>>") that are
> red
>> flags to fundamental misunderstandings on awk semantics.
>
> Ed. I will certainly take your advice to get myself more into awk.
>
> BTW, I tried out (print> file) and it worked and I guess it has to do
> with awk being working on stream.
The issue is that the semantics of > and >> are different in awk than in shell.
You should be using > in your script.
>
> On the "if (..)" part, let me guess ... I should not use $3 due to
> the fact that not every line has $3 ... isn't that what I did wrong?
>
> Or you meant something else?
>
Something else. Awk scripts are made up of:
<condition> { <action> }
statements. So you naturally should put your conditions in the condition part
rather than the action part. i.e. instead of:
{ if (<condition>) { <action> } }
you should simply write:
<condition> { <action> }
unless there's a good reason not to do that (which in this case there is not).
> Thanks
>
You're welcome.
Ed.