running a system command in kdb

Viewed 127

I am trying to run the command in kdb but it does not work. I knew it may be associated with some special character and I am trying to put the [] to escape but still does not work.

system "awk '/^Mem/ {print $2}' <(free -m)"

I tried

system "awk '/[^]Mem/ {print $2}' <(free -m)"  - not working

any input would be appreciated.

2 Answers

You can avoid a system call as kdb+ can return the physical memory available using .Q.w[]

q)floor (.Q.w[]`mphy)%1024 xexp 2
28072
\\
$ awk '/^Mem/ {print $2}' <(free -m)
28072

Another solution just requires a rearrangement of your expression:

q)system"free -m | awk '/^Mem/ {print $2}'"
"25408"

EDIT:

The reason why your expression is failing from q is because of the shell being used. This answer explains the difference between shebang and dash shells. I've added a little test to showcase the difference.

coneill5@LPTP1893: [~] $ cat test.sh
#!/bin/bash
awk '/^Mem/ {print $2}' <(free -m)
coneill5@LPTP1893: [~] $ cat test1.sh
#!/bin/sh
awk '/^Mem/ {print $2}' <(free -m)
coneill5@LPTP1893: [~] $ ./test.sh
25408
coneill5@LPTP1893: [~] $ ./test1.sh
./test1.sh: 2: Syntax error: "(" unexpected
Related