Command Line Tip

Often times you may come up with some really cool shell command to get the exact kind of data you looking for only to find out that when you parse it into a for loop, the command falls a part:

Case A – My Cool Command

# Get’s a print out of the disk information without the leading /dev/

 df -h | grep ‘hd|sd’ | sed s/\/dev\///

*NB. To escape a character in sed, two backslashes are required.

Case B – Now My Cool Command in For Loop

for i in `df -h | grep ‘hd|sd’ | sed s/\/dev\///`; do  echo $i; done

*NB. Requires Three backslashes are required

Don't be shellfish...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

Awk is Amazing

Without a doubt, Awk is beautiful.

I’ve been neglecting linux now for about the past 2 years, after spending 10 years pursuing it with much passion; and I was again reminded of why I enjoyed linux. It was the continual problems, and experiences the highs of solving them. Bearing that in mind, there are too many problems in that space and now  I just prefer Windows for it’s focus away from the OS to the application – in this case my IDE.

That’s a bit of a tangent, but coming back to Awk; I am reminded of how powerful it is, and how Windows lags behind in having this kind of power on the command line (natively).

 

# Print disk stats from the third field

cat /proc/diskstats | awk ‘{ print substr($0, index($0,$3))}’

# Print disk stats of those whose last field is not zero

cat /proc/diskstats | awk ‘$NF != 0’

*sigh*
~ ooooh
for i in `cat /proc/diskstats | awk ‘$NF != 0’ | awk ‘{ print substr($0, index($0,$3))}’`; do echo $i; done
Just what I needed 😉

 

Don't be shellfish...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone