Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132

    Question cat /proc/swaps: Calculate kilobytes into megabytes


    I read on a webpage (that I cannot recall) that the output of cat /proc/swaps ( or swapon -s ) is in Kilobytes.

    Now, I want that info converted into megabytes, and best, the same way that free calculates its standard no-arguments give output of KB memory into MB.

    Now, I know there are several ways to do so, like using sed or awk or perl or such. I would like to have a solution that uses the minimal approach, e.g. the smallest and most widely used programs that are installed on nearly all Linux systems. (As in: better just using bash, or sed, or awk, but not perl or python or such)

    analysing how /proc/swaps is written (using mcedit) I give you its output, it uses <---><------> to show tabs(aka tab stop, tabulator, tab character or hex value 09), so don't take the <---> parts literally, but read them as equivalent to tabs.

    Code:
    Filename<------><------><------><------>Type<--><------>Size<-->Used<-->Priority
    Now, this is nicely done, but sadly, only the leading line uses tabs after the filename, the entries themselves look like so:
    Code:
    /dev/zram0                              partition<----->695938<>1024<----->100
    Now, the two entries here should be converted from KB into MB: 695938 and 1024

    And every line but the first should be converted.

    Any ideas what would be the most efficient (and failsafe) way in doing so?

    Me thinks, using whitespace and tabs as seperators for field entries is how most programs work, so it should hopefully be no issue that seemingly the entry lines only use whitespace to separate the Filename and Type field, but then use tabs to separate all following fields.

    So, calculating the entries starting from 1 for the first one, the entries to be calculated should be #3 and #4 (for Size & Used, respectively)

    So, just calculating every numbered entry would not be correct, since the Priority entry, or field #5, should not be altered.
    Cheers!
    yours truly, Rava

  2. #2
    pretty easy !

    oh, btw, hello,

    still quiet easy with bash:
    Code:
    $ { read firstLine; read f t s u p;} </proc/swaps
    $ echo $s

  3. #3
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132
    Hi watael,

    Thanks, but it is only reading the first info line. On my current system I have 3 kind of swaps: A swap partition, A zram as swap, and a swapfile.

    Your solution only works on the very first line and discards all following.

    Here, have a swaps file to check code with:
    Code:
    Filename				Type		Size	Used	Priority
    /dev/sda1                               partition	2097148	2010	1
    /dev/zram0                              partition	695938	8100	100
    /mnt/sda4/swap.file                     file		3686396	2018	-1
    Cheers!
    yours truly, Rava

  4. $spacer_open
    $spacer_close
  5. #4
    as easy as previously:
    Code:
    { read firstLine; while read f t s u p; do echo $s; done;} < /proc/swaps

  6. #5
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132
    Okay, that does the echo'ing, but I want all lines copied, just the entries $s and $u divided by 1024. What do you suggest I use? That can be done by bash buildin alone if I am not mistaken, yes?

    But the issue with your method, as I see it, when changing the MB to KB, the length of these numbers do change as well, so it will happen that one tab is not enough, but two are needed after one of these numbers. It would be quite the messy script to get all the calculating done to be failsafe, so that all numbers are still in the same rows and the title from $firstLine still fits (if that is even reading the tabs as it should, without any conversation. Which it does not:
    Code:
    # { read firstLine; echo $firstLine; while read f t s u p; do echo $f $t $s $u $p; done;} < /proc/swaps
    Filename Type Size Used Priority
    /dev/sda1 partition 2097148 2010 1
    /dev/zram0 partition 695938 8100 100
    /mnt/sda3/pagefile.sys file 3686396 2018 -1
    Of course, one could just add the -e to the echo, and a tab ( aka "/t" ) after each string... but that would be in no way as failsafe as the way /proc/swaps is created...


    Therefore I would prefer a way to convert each line, just convert the number at a certain field position, while leaving all whitespace, be them whitespace or horizontal tab just as it is,

    Like I said above, that is still not failsafe with sometimes a 2nd tab needed after a shorter number...
    Cheers!
    yours truly, Rava

  7. #6
    replace echo with
    Code:
     printf '%-30s%-30s%-30s%-30s%-30s\n' $f $t $s $u $p
    and adapt numeric values to fit your needs.

  8. #7
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132
    thanks a lot for the heads up, did not know about that formatting stuff with printf, at least my man printf don't tell me about any of that. Me thinks you knew it from C?

    Here is the code so far:
    Code:
    head -n 1 /proc/swaps; { read firstLine; while read f t s u p; do printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s $u $p; done;} < /proc/swaps
    I use the head -n so that I later would not be bothered when I start with the calculating of %s and %u, cause when I use that one the first line it gets more messy than this approach...
    Cheers!
    yours truly, Rava

  9. #8
    Quote Originally Posted by Rava View Post
    Me thinks you knew it from C?
    nope, learned it from Chris Johnson on a forum, or IRC.
    i just spread the word.

  10. #9
    Linux Newbie Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    132
    Quote Originally Posted by watael View Post
    nope, learned it from Chris Johnson on a forum, or IRC.
    i just spread the word.
    Well, what ( did learn is that you learn a lot by reading man pages and stuff, but you never learn everything. The most neat stuff I learned was from forum geeks or good books.


    Anyhow, here the final version. What do you think of it?

    Code:
    head -n 1 /proc/swaps; { read firstLine; while read f t s u p; do let "s2 = $s / 1024" ; let "u2 = $u / 1014"; printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p; done;} < /proc/swaps
    Cheers!
    yours truly, Rava

  11. #10
    -->
    I would not head the file, I'd simply
    Code:
    echo "$firstLine"
    the quotes are important to keep the formatting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •