1 |
On Fri, 19 Nov 2004 06:21:47 +0000 (GMT) Ed Grimm |
2 |
<paranoid@××××××××××××××××××××××.org> wrote: |
3 |
| cat | grep | (sed|awk) lines have always bothered me to no end. How |
4 |
| about, instead, one of the following function bodies: |
5 |
| |
6 |
| awk '/MemTotal/ { print $2}' /proc/meminfo |
7 |
| sed -r -e '/MemTotal/!d' -e 's/[^0-9]*([0-9]+).*/\1/' |
8 |
| /proc/meminfo perl -ne '/MemTotal:\s+(\d+)/ && print "$1\n"' |
9 |
| /proc/meminfoo grep MemTotal | (read tag size units; echo $size) |
10 |
| |
11 |
| Normally, I wouldn't bring it up, but there's *so* many ways to do |
12 |
| that better, it's sad. Note that I could've listed many more, but |
13 |
| that would've been sad as well. (Actually, even this much is somewhat |
14 |
| sad.) Personally, I'd vote for the first or third of this group. |
15 |
|
16 |
Best to avoid awk, it's horridly unportable between implementations, and |
17 |
if possible it's better to avoid perl unless you actually need it. The |
18 |
sed solution is safest, and it's probably nicer to do it in one command: |
19 |
|
20 |
sed -n -e '/MemTotal:/s/^[^:]*: *\([0-9]\+\).*/\1/p' /proc/meminfo |
21 |
|
22 |
Maybe I should make my "cat abuse" remarks less subtle next time :) |
23 |
|
24 |
-- |
25 |
Ciaran McCreesh : Gentoo Developer (Vim, Fluxbox, Sparc, Mips) |
26 |
Mail : ciaranm at gentoo.org |
27 |
Web : http://dev.gentoo.org/~ciaranm |