PDA

View Full Version : Bash Script use of Sed here is an easy one


dhonnoll78
22nd February 2008, 17:31
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
74
Drive 2 74
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

falko
23rd February 2008, 18:06
You could use the cut command: man cut

KenJackson
9th March 2008, 06:21
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.

make-fun
5th April 2008, 08:28
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
and
df -h /dev/hda1 | awk 'NR == 2 {print "Drive 2 " $3}'
Drive 1 13M
just replace the hda1… with what ever you have/need
Cheers