Tuesday, 6 September 2016

Copying command line output to CSV file

This post may seem trivial to many system administrators. But I decided to post it any way because I think it's cool.

So, in our daily routine many times we have to gather information from many servers & submit it in an excel report to be shared with management or our supervisors.

Here's a quick hack which might possibly help save some of the effort spent reporting.

I took the output of df -h & put it in a file called df.txt. The file looks like this:

[root@devbox ~]# cat df.txt
Filesystem             Size  Used Avail Use% Mounted on
/dev/sda3               18G  6.3G   12G  36% /
devtmpfs               482M     0  482M   0% /dev
tmpfs                  490M   80K  490M   1% /dev/shm
tmpfs                  490M  7.1M  483M   2% /run
tmpfs                  490M     0  490M   0% /sys/fs/cgroup
/dev/mapper/repo-epel  5.0G  117M  4.9G   3% /epel
/dev/sda1              297M  106M  192M  36% /boot


Then I pass this file through an awk code snippet & save the output in a file called df.csv.

 awk '$1=$1' FS=" " OFS="," df.txt > df.csv

With the above code snippet I replaced the space bar delimiter with a comma. So the file df.csv looks like this:

[root@devbox ~]# cat df.csv
Filesystem,Size,Used,Avail,Use%,Mounted,on
/dev/sda3,18G,6.3G,12G,36%,/
devtmpfs,482M,0,482M,0%,/dev
tmpfs,490M,80K,490M,1%,/dev/shm
tmpfs,490M,7.1M,483M,2%,/run
tmpfs,490M,0,490M,0%,/sys/fs/cgroup
/dev/mapper/repo-epel,5.0G,117M,4.9G,3%,/epel
/dev/sda1,297M,106M,192M,36%,/boot

That isn't very cool right ?

Well, I copied this file to my desktop using winscp & then opened it with Microsoft office & looks like this:


And that is cool !

I hope this helps save someone some time & and escape from the boredom of repetitive activity.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...