LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 05-28-2010, 03:28 PM   #1
dynaguy
LQ Newbie
 
Registered: May 2010
Posts: 9

Rep: Reputation: 1
What is wrong with the "sed"?


Hello, all,

I have a backup script running as a cronjob. The script check the used disk space before start to backup. The command it use is
Code:
df /daily | tail -1 | sed -e 's/^[^ ]* *[0-9]* \([0-9]*\).*$/\1/'
I verified this in terminal and here is the result:
Code:
sysadmin@beta:~$ df /daily
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdc1            969066524 852215832  68012564  93% /daily
sysadmin@beta:~$ df /daily | tail -1 | sed -e 's/^[^ ]* *[0-9]* \([0-9]*\).*$/\1/'
852215832
Now I try to use same script on a different server. But it can't get the used space number any more. The out put is like this on the 2nd server:
Code:
sysadmin@delta:~$ df /emdata
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sdb1            721078300  77859612 606589960  12% /emdata
sysadmin@delta:~$ df /emdata | tail -1 | sed -e 's/^[^ ]* *[0-9]* \([0-9]*\).*$/\1/'
Here I just got a blank out put.

I think it is the sed command cause the problem but I can't figure it out what is wrong with it. Can someone help me please?

Last edited by dynaguy; 05-28-2010 at 03:30 PM.
 
Old 05-28-2010, 03:41 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Why use sed:

df /emdata | awk '/\/emdata/ { print $3 }'

Hope this helps.
 
1 members found this post helpful.
Old 05-28-2010, 03:55 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I second druuna's advice. Anyway, the reason why the sed regexp does not work in the second example is that you missed an asterisk:
Code:
sed -e 's/^[^ ]* *[0-9]*_\([0-9]*\).*$/\1/'
the space underlined in red matches only a single space between the first and the second number. This is true in your first example, false in the second one where there are two spaces. In any case the awk solution is far easier and readable.
 
2 members found this post helpful.
Old 05-28-2010, 05:50 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,276

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
sed rarely causes problems - now, regex on the other hand ...
IMHO relying on specific whitespace is very poor programming practice - as shown, awk avoids the issue here.
 
Old 05-28-2010, 06:28 PM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Always use df -P in scripts.

Here's an example of what can happen if you don't:
Code:
gazl@nix:~$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/rootvg/lvroot      516040    429644     60184  88% /
/dev/sda1              4047904     44852   3797424   2% /boot
/dev/mapper/rootvg-lvusr
                      16513960   6660584   9014516  43% /usr
/dev/mapper/rootvg-lvopt
                        516040     16780    473048   4% /opt
As you can see, some lines are split into two, which is probably the reason you're getting a blank line.

df -P will give you one line per filesystem no matter what:
Code:
gazl@nix:~$ df -P
Filesystem         1024-blocks      Used Available Capacity Mounted on
/dev/rootvg/lvroot      516040    429644     60184      88% /
/dev/sda1              4047904     44852   3797424       2% /boot
/dev/mapper/rootvg-lvusr  16513960   6660584   9014516      43% /usr
/dev/mapper/rootvg-lvopt    516040     16780    473048       4% /opt
 
1 members found this post helpful.
Old 05-31-2010, 03:58 PM   #6
dynaguy
LQ Newbie
 
Registered: May 2010
Posts: 9

Original Poster
Rep: Reputation: 1
Thanks for all your help.

I now just use awk instead. Like this:
Code:
df /emdata | tail -1 | awk '{print $3}'
 
Old 05-31-2010, 05:50 PM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 3,392

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by dynaguy View Post
Thanks for all your help.

I now just use awk instead. Like this:
Code:
df /emdata | tail -1 | awk '{print $3}'
When I advised using 'df -P' it wasn't just idle fancy. Look what would happen if you tried that code on my box:
Code:
gazl@nix:~$ df /boot /usr  
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1              4047904     44852   3797424   2% /boot
/dev/mapper/rootvg-lvusr
                      16513960   6660584   9014516  43% /usr

gazl@nix:~$ df /boot | tail -1 | awk '{ print $3 }'     
44852

gazl@nix:~$ df /usr | tail -1 | awk '{ print $3 }'
9014516
See what happened? It's quite possible your script will read the wrong value under certain circumstances.

And now, the correct way:
Code:
gazl@nix:~$ df -P /boot | tail -1 | awk '{ print $3 }'
44852

gazl@nix:~$ df -P /usr | tail -1 | awk '{ print $3 }'
6660584
 
2 members found this post helpful.
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Debian / Wrong free space / "du" and "ls" show different space use brgsousa Linux - Software 3 02-25-2010 11:55 AM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
dhcp says "wrong interface name: "ath0" Quakeboy02 Linux - Networking 10 12-19-2006 07:14 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM


All times are GMT -5. The time now is 07:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration