Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Assignment to a parameter

15 views
Skip to first unread message

Roberto Zuelli

unread,
Oct 12, 2012, 9:53:56 AM10/12/12
to
Hello,

I want to assigne to each node a parameter (called type) that
represents its type.

I have a txt file like this:
Node Area Pd Ls
AA £ AQ 0.00 0.00
BB £ BW 0.00 0.00
CC £ BE 10.00 0.00

I want to assigne to the first node of the file with Pd=0.00 and
Ls=0.00: type=0.
For the remaining nodes, also if they have Pd=0 and Ls=0, type=1.

Any idea?

Thanks

Best regards

Roberto

Janis Papanagnou

unread,
Oct 12, 2012, 10:18:54 AM10/12/12
to
Check whether that matches your needs...

awk '
NR==1 { print $0 "\tType" ; next }
!( ($4,$5) in type ) { type[$4,$5] = ++c }
NR>1 { print $0 "\t" type[$4,$5] }
' txt-file


Janis

>
> Thanks
>
> Best regards
>
> Roberto
>

Roberto Zuelli

unread,
Oct 12, 2012, 10:27:30 AM10/12/12
to
On Oct 12, 3:18 pm, Janis Papanagnou <janis_papanag...@hotmail.com>
wrote:
> > Roberto- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

the problem is that I do not know the number of the row where the node
with the desired characteristic(Pd=0 and Ls=0) is.
Casually in the example I wrote it is in the first row.

Janis Papanagnou

unread,
Oct 12, 2012, 10:45:26 AM10/12/12
to
I seem to have misinterpreted your requirements. How about...

awk '
NR==1 { print $0 "\tType" ; next }
NR>1 { print $0 "\t" !($4==0 && $5==0 && !seen++) }
' txt-file


Janis

>

Dave Gibson

unread,
Oct 12, 2012, 1:17:28 PM10/12/12
to
Roberto Zuelli <rober...@gmail.com> wrote:
> Hello,
>
> I want to assigne to each node a parameter (called type) that
> represents its type.
>
> I have a txt file like this:
> Node Area Pd Ls
> AA � AQ 0.00 0.00
> BB � BW 0.00 0.00
> CC � BE 10.00 0.00
>
> I want to assigne to the first node of the file with Pd=0.00 and
> Ls=0.00: type=0.
> For the remaining nodes, also if they have Pd=0 and Ls=0, type=1.

awk '
NR == 1 { print $0 " Type" ; next }
$4 + $5 == 0 { $0 = $0 " " t + 0 ; t = 1 }
{ print }
' your_file

Not sure about the spacing.

Janis Papanagnou

unread,
Oct 12, 2012, 1:27:21 PM10/12/12
to
If Pd or Ls values could be negative, and are, say something like
2.00 and -2.00, then this condition will fail. It's safer to not
assume that and write

$4==0 && $5==0

or if you want to save characters and we assume non-empty numerical
values in $4 and $5 (which seems to be a safer assumption) then

!$4 && !$5

would suffice.

Janis

Dave Gibson

unread,
Oct 12, 2012, 2:50:07 PM10/12/12
to
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
> On 12.10.2012 19:17, Dave Gibson wrote:
>> Roberto Zuelli <rober...@gmail.com> wrote:
>>> Hello,
>>>
>>> I want to assigne to each node a parameter (called type) that
>>> represents its type.
>>>
>>> I have a txt file like this:
>>> Node Area Pd Ls
>>> AA £ AQ 0.00 0.00
>>> BB £ BW 0.00 0.00
>>> CC £ BE 10.00 0.00
>>>
>>> I want to assigne to the first node of the file with Pd=0.00 and
>>> Ls=0.00: type=0.
>>> For the remaining nodes, also if they have Pd=0 and Ls=0, type=1.
>>
>> awk '
>> NR == 1 { print $0 " Type" ; next }
>> $4 + $5 == 0 { $0 = $0 " " t + 0 ; t = 1 }
>
> If Pd or Ls values could be negative, and are, say something like
> 2.00 and -2.00, then this condition will fail.

Aargh!

> It's safer to not assume that and write
>
> $4==0 && $5==0

Phew, thanks.
0 new messages