Solved

Replace all spaces by,

Posted on 2011-03-15
6
398 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.

948 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

20 Experts available now in Live!

Get 1:1 Help Now