How to print a specific column with awk on a remote ssh session?

When using

df -h | grep /dev/root | awk '{print $5}'

I get the usage of my SD Card in my Pi: 78%

But when I use

/usr/bin/ssh -i /path/to/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f3f5e3f4c6f5e3f4f0e3f4">[email protected]</a> "df -h | grep /dev/root | awk '{print $5}'"

from another computer, I get:

/dev/root       7.2G  5.3G  1.6G  78% /

Full df -h:

Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.2G  5.3G  1.6G  78% /
devtmpfs        364M     0  364M   0% /dev
tmpfs           368M   68K  368M   1% /dev/shm
tmpfs           368M  5.2M  363M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           368M     0  368M   0% /sys/fs/cgroup
/dev/mmcblk0p1   60M   21M   40M  35% /boot
tmpfs            74M  4.0K   74M   1% /run/user/1000

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You’re hitting a quoting problem; the $5 is being interpreted at the wrong time. There are at least two solutions:

  1. Put a before the $; e.g.
    /usr/bin/ssh -i /path/to/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="334640564173405641455641">[email protected]</a> "df -h | grep /dev/root | awk '{print $5}'"
  2. Run the df remotely but the grep and awk locally. e.g.
    /usr/bin/ssh -i /path/to/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e0e6f0e7d5e6f0e7e3f0e7">[email protected]</a> df -h | grep /dev/root | awk '{print $5}'

FWIW, I’d run a version of the second option but merging grep and awk

/usr/bin/ssh -i /path/to/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e1b1d0b1c2e1d0b1c180b1c">[email protected]</a> df -h | awk '//dev/root/ {print $5}'

Method 2

This should work:

/usr/bin/ssh -i /path/to/key <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="156066706755667067637067">[email protected]</a> "df -h | grep /dev/root | awk '{print $5}'"

Notice the included before the $. Without this, the local shell will expand the empty variable $5 and send that to the remote server. Essentially, printing the entire line.

Method 3

For completeness, another way is to use the fact $n in awk isn’t a special case of $variable syntax like shell, but instead the $ operator applied to an integer expression:

(ssh key&remote) "df -h | grep /dev/root | awk '{print $ 5}'"

or combined as

(ssh key&remote) "df -h | awk '/\/dev\/root/ {print $ 5}'"
# can use [/] instead of ugly \/ in gawk, but maybe not others

or maybe better as a variable

(ssh key&remote) "df -h | awk -vm=/dev/root '$ 0 ~ m {print $ 5}'"

But personally I’d go with Stephen Harris’ preference to run the awk locally.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x