Bash Script use of Sed here is an easy one

Discussion in 'Programming/Scripts' started by dhonnoll78, Feb 22, 2008.

  1. dhonnoll78

    dhonnoll78 New Member

    I have a line of sed code in a script that is to pull from a .txt file that looks like this:
    /dev/sda= 74 GB
    /dev/sdb= 74 GB

    drive1=`cat /tmp/drives.txt | sed -e 's/^.* \([[:digit:]]\{2,\}\) GB.*/\1/' `
    drive2=`cat /tmp/drives.txt | sed -e 's/^.* \([[:digit:]]\{2,\}\) GB.*/\1/' `
    echo "Drive 1 $drive1">>/tmp/mynewfile.txt
    echo "Drive 2 $drive2">>/tmp/mynewfile.txt

    The output looks like this:
    Drive 1 74
    Drive 2 74

    Is it possible to get sed to find the first digit, yank it as a variable and stop, then use sed to find the second digit yank it as a separate variable and stop?
    I want it to look like this
    Drive 1 74
    Drive 2 74
    I am only interested in that 74 really or 34 in some cases which is what I am really trying to find out
    Last edited: Feb 22, 2008
  2. falko

    falko Super Moderator ISPConfig Developer

    You could use the cut command:
    man cut
  3. KenJackson

    KenJackson New Member

    Try this:
    sed -ne '/sda/s/^.* \([[:digit:]]\{2,\}\) GB.*/Drive 1 \1/p' /tmp/drives.txt >>/tmp/mynewfile.txt
    sed -ne '/sdb/s/^.* \([[:digit:]]\{2,\}\) GB.*/Drive 2 \1/p' /tmp/drives.txt >>/tmp/mynewfile.txt
    The leading /sda/ and /sdb/ are addresses--they mean the s command does not take effect unless the address matches.

    The -n switch causes nothing to be printed unless a 'p' flag takes affect (added at the end), which it doesn't unless the address matches.
  4. make-fun

    make-fun Member

    It seems to be about the local HD,

    so this should work too:
    df -h /dev/hda3 | awk 'NR == 2 {print "Drive 1 " $3}'
    Drive 1 17G
    df -h /dev/hda1 | awk 'NR == 2 {print "Drive 2 " $3}'
    Drive 1 13M
    just replace the hda1… with what ever you have/need

Share This Page