LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   need help w/ awk & printf (https://www.linuxquestions.org/questions/programming-9/need-help-w-awk-and-printf-180376/)

rickenbacherus 05-11-2004 01:51 PM

need help w/ awk & printf
 
This very simple script works fine:
Code:

echo -n "Kernel:"
uname -a|awk '{print $1, $3}'
echo -n "CPU:"
cat /proc/cpuinfo|grep name|awk '{print $4, $5, $6, $7}'
cat /proc/cpuinfo|grep MHz|awk '{print $2, $3, $4}'
echo -n "Mem/Use:" && free|grep Mem:|awk '{print $2, $3}' && echo -n "Uptime:"
uptime|awk '{print $3}'
echo -n "Resolution:"

When executed prints this:

Kernel:Linux 2.6.5
CPU:AMD Athlon(TM) XP 2200+
MHz : 1795.664
Mem/Use:1002320 188820
Uptime:5:41


I would however like to change the format so that it looks like this:

Kernel:Linux 2.6.5 CPU:AMD Athlon(TM) XP 2200+ @ 1795.664 MHz
Mem/Use:1002320 188820 Uptime:5:41

So far I have only managed to make the format extremely ugly. :(
Thanks alot for any pointers! :)

david_ross 05-11-2004 02:00 PM

It works with backticks:
Code:

echo "Kernel: `uname -a|awk '{print $1, $3}'` CPU: `cat /proc/cpuinfo|grep name|awk '{print $4, $5, $6, $7}'` @ `cat /proc/cpuinfo|grep MHz|awk '{print $4" "$2}'`"
echo "Mem/Use: `free|grep Mem:|awk '{print $2, $3}'` Uptime: `uptime|awk '{print $3}'`"

or perhaps easier to read:
Code:

echo -n "Kernel: `uname -a|awk '{print $1, $3}'`"
echo -n " CPU: `cat /proc/cpuinfo|grep name|awk '{print $4, $5, $6, $7}'`"
echo " @ `cat /proc/cpuinfo|grep MHz|awk '{print $4" "$2}'`"
echo -n "Mem/Use: `free|grep Mem:|awk '{print $2, $3}'`"
echo " Uptime: `uptime|awk '{print $3}'`"


Hko 05-11-2004 02:06 PM

Code:

#!/bin/bash

KERNEL=$(uname -a|awk '{print $1, $3}')
CPU=$(cat /proc/cpuinfo|grep name|awk '{print $4, $5, $6, $7}')
CLK=$(cat /proc/cpuinfo | awk -F': ' '/MHz/{print $2}')
MEM=$(free|grep Mem:|awk '{print $2, $3}')
UPTIME=$(uptime|awk '{print $3}')

echo "Kernel:$KERNEL CPU:$CPU@ $CLK MHz"
echo "Mem/Use:$MEM Uptime:$UPTIME"


rickenbacherus 05-13-2004 07:21 AM

:) Perfect! Thanks alot.


All times are GMT -5. The time now is 11:12 AM.