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.
Reply
 
Thread Tools Display Modes
  #1  
Old 22nd February 2008, 16:31
dhonnoll78 dhonnoll78 is offline
Member
 
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
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

Last edited by dhonnoll78; 22nd February 2008 at 16:44.
Reply With Quote
Sponsored Links
  #2  
Old 23rd February 2008, 17:06
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,711
Thanks: 1,899
Thanked 2,702 Times in 2,545 Posts
Default

You could use the cut command:
Code:
man cut
__________________
Falko
--
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
  #3  
Old 9th March 2008, 05:21
KenJackson KenJackson is offline
Junior Member
 
Join Date: Jul 2006
Location: Maryland, USA
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:
Code:
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
  #4  
Old 5th April 2008, 07:28
make-fun make-fun is offline
Member
 
Join Date: Jan 2008
Posts: 92
Thanks: 8
Thanked 8 Times in 7 Posts
 
Default

It seems to be about the local HD,

so this should work too:
Code:
df -h /dev/hda3 | awk 'NR == 2 {print "Drive 1 " $3}'
Drive 1 17G
and
Code:
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
Reply With Quote
Reply

Bookmarks

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 21:08
https protocol stops page load tjd General 7 18th November 2007 18:06
Help Stats Webalizer jfruano Installation/Configuration 2 27th February 2007 13:03
bash script compare variables pattern matching tombstone Programming/Scripts 1 1st June 2006 00:27
req: Bash / Script to auto kill PID if it's needed.... edge Programming/Scripts 8 13th March 2006 23:31


All times are GMT +2. The time now is 00:43.


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