Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Thread Tools Display Modes
Old 22nd February 2008, 17:31
dhonnoll78 dhonnoll78 is offline
Join Date: Dec 2006
Posts: 42
Thanks: 2
Thanked 0 Times in 0 Posts
Default Bash Script use of Sed here is an easy one

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 by dhonnoll78; 22nd February 2008 at 17:44.
Reply With Quote
Sponsored Links
Old 23rd February 2008, 18:06
falko falko is offline
Super Moderator
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,751 Times in 2,581 Posts

You could use the cut command:
man cut
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
Old 9th March 2008, 06:21
KenJackson KenJackson is offline
Junior Member
Join Date: Jul 2006
Location: Maryland, USA
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts

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.
Reply With Quote
Old 5th April 2008, 08:28
make-fun make-fun is offline
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts

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
Reply With Quote


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sql ledger using forum.. yurtboy1 General 19 25th December 2010 22:08
https protocol stops page load tjd General 7 18th November 2007 19:06
Help Stats Webalizer jfruano Installation/Configuration 2 27th February 2007 14:03
bash script compare variables pattern matching tombstone Programming/Scripts 1 1st June 2006 01:27
req: Bash / Script to auto kill PID if it's needed.... edge Programming/Scripts 8 14th March 2006 00:31

All times are GMT +2. The time now is 05:36.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.