Solved

Replace all spaces by,

Posted on 2011-03-15
6
409 Views
Last Modified: 2012-05-11
Hi,

Can I get a perl script that can convert the following output into CSV. So basically all the spaces need to be converted to a comma.

My goal is to run a cron job every day and then add the output of df-h into a CSV file every day. I need to script.

Script 1 will just convert this output to CSV

Script 2 will open a file everyday for append and then it will issue the df -h command then store the output of df -h into a CSV file everyday  and it will skip the headers. Also can the perl script get the hostname of the machine also and then add it as a column to the csv and repeat the hostname in the CSV every day ?



thanks
-anshu

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G  4.0G  9.8G  29% /
/dev/sda6              27G  6.9G   19G  28% /omega
/dev/sda3             4.9G  390M  4.3G   9% /var
/dev/sda5             4.9G  139M  4.5G   3% /tmp
tmpfs                 127G     0  127G   0% /dev/shm
/dev/sdb2              35G   15G   19G  44% /alpha
/dev/sdc1             135G   28G  100G  22% /alpha1
/dev/sdd1             135G  105G   23G  83% /alpha2
0
Comment
Question by:anshuma
  • 2
  • 2
  • 2
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 35144571

perl -i.bak -lpe 's/\s+/,/g' file
0
 

Author Comment

by:anshuma
ID: 35144586
Hi Ozo,

Thank you very much. How can I make it skip the first line which is

Filesystem            Size  Used Avail Use% Mounted on


thanks
-anshu
0
 
LVL 14

Expert Comment

by:sentner
ID: 35144670
Easiest way to skip the first line is to just pipe the df output first to tail +2

df -h | tail +2 | perl -lpe 's/\s+/,/g'
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:anshuma
ID: 35144956
[root@myserver ~]# df -h | tail +2 | perl -lpe 's/\s+/,/g' > x1.txt
tail: cannot open `+2' for reading: No such file or directory

I get this error
0
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 35145997
df -h | perl -lpe '$_=<> if 1..1;s/\s+/,/g'
0
 
LVL 14

Assisted Solution

by:sentner
sentner earned 150 total points
ID: 35150492
Sorry, tail +2 doesn't work for every version of tail.

You should be able to use:

df -h | tail --lines=+2 | perl -lpe 's/\s+/,/g'

Or:

df -h | egrep -v "^Filesystem"| perl -lpe 's/\s+/,/g'


Ozo's last comment seems to address your need, though when I try it it adds an extra comma to the end of the first line.  
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to display a matrix report for 3-dimenional array 3 66
syslog unix file 20 71
PERL variable conundrum 9 90
quoting a comma separated list 20 84
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now