Solved

Replace all spaces by,

Posted on 2011-03-15
6
386 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

18 Experts available now in Live!

Get 1:1 Help Now