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

Running awk from Matlab system command

447 views
Skip to first unread message

Bruce Clarke

unread,
Feb 6, 2011, 5:17:03 PM2/6/11
to
Hi,
I can run the following awk command from within Matlab (it counts the number of unique entries in column 2):

system(['C:\cygwin\bin\gawk -F, ''NR!=1 { exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]);

However, when I add an if statment with > or < the Matlab system command strips this off and hence the command fails. For example the following fails in Matlab (but works directly within cygwin):

system(['C:\cygwin\bin\gawk -F, ''NR!=1 { if ($9<10) exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]);

Is there a way of stopping Matlab from removing > or <?

Thanks

TideMan

unread,
Feb 6, 2011, 5:48:53 PM2/6/11
to
On Feb 7, 11:17 am, "Bruce Clarke" <bruce.cla...@team.telstra.com>
wrote:

Well, I don't believe you.
I use system all the time with similarly complicated commands and
Matlab simply doesn't misbehave in the fashion you assert.
May I suggest that you do the following:
comm= (['C:\cygwin\bin\gawk -F, ''NR!=1 { if ($9<10) exchNo[$2]+=1 }


END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]

and check that what is printed out is exactly what you expect.
Then you can do this:
system(comm)

Bruce Clarke

unread,
Feb 6, 2011, 7:03:05 PM2/6/11
to
TideMan <mul...@gmail.com> wrote in message <10f7fb02-c1c5-4e93...@o39g2000prb.googlegroups.com>...

Hi,
Thanks for the suggestion. Actually this is exactly what I do but get a syntax error:

gawk: NR!=1 { if ($9 exchNo[$2]+=1 }END { for ( id in exchNo ) print id,exchNo[id] }
gawk: ^ syntax error

As you can see some text following the < is removed resulting in the error. If I eliminate the if statement then it works and if I replace < by = then this also works. So it appears that the system command is removing the <.

TideMan

unread,
Feb 6, 2011, 7:19:35 PM2/6/11
to
On Feb 7, 1:03 pm, "Bruce Clarke" <bruce.cla...@team.telstra.com>
wrote:
> TideMan <mul...@gmail.com> wrote in message <10f7fb02-c1c5-4e93-90c9-cd2b9e87e...@o39g2000prb.googlegroups.com>...

When I use sed, I use double quotes,char(34), not apostrophe
apostrophe, char(39) char(39).
Maybe Matlab is getting confused with a string within a string.

Bruce Clarke

unread,
Feb 6, 2011, 11:06:04 PM2/6/11
to
TideMan <mul...@gmail.com> wrote in message <66b4375a-e96f-4ba4...@i39g2000prd.googlegroups.com>...

Hi,
Thanks Tideman you were correct. I was using ' to escape ' and this has been working fine for my applications except when I tried the conditional if statement and the awk no longer worked. By replacing two apostrophes '' (i.e. char(39)char(39)) with the double quotes " (i.e. char(34)) the awk command works.

In my search for using awk in Matlab a number of sites recommend using the two apostrophes but the example above suggests it is better to use double quotes.

So for completeness here is the correct code to count the number of unique occurrences in column 2 of a csv file (Filename) given that column 9 must be less than a value of 10:

comm= ['C:\cygwin\bin\gawk -F, "NR!=1 { if ($9<10) exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } " ' Filename ];

output=system(comm)

Again Thanks for your help

0 new messages