ayyappancheta
ayyappancheta

Reputation: 1

text to csv conversion

I have an output of the Linux command "who" which provides the following details.....

CURRENT USER/ACCT INFO
17:31:36 up 4:49, 4 users, load average: 0.03, 0.04, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root :0 - 12:59 ?xdm? 4:54 0.02s /bin/sh /usr/bi
root pts/0 :0 12:59 4:31m 0.00s 1:20 kded [kdeinit] 
root pts/1 :0.0 16:18 1.00s 0.00s 0.00s -bash
root pts/2 :0.0 16:25 49.00s 0.02s 0.00s bash

This output I saved into a file named WHO.log

Now how to convert this output into CSV format so that I can export it into some database, using some bash script?

Upvotes: 0

Views: 760

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753870

Assuming the command is 'who' (are you sure?), then you need the space separated columns 1-7 output as comma separated fields 1-7, but you need columns 8 and later treated as one output field. That is probably best handled with the shell read command. I'm assuming that the first three lines should be discarded.

who | sed '1,3d' |
while read user tty from login idle jcpu pcpu what
do echo "$user,$tty,$from,$login,$idle,$jcpu,$pcpu,$what"
done

Upvotes: 0

Dennis Williamson
Dennis Williamson

Reputation: 360105

Try this:

your_who_command | awk '{$1=$1; print}' OFS=,

Upvotes: 1

grncdr
grncdr

Reputation: 956

Completely untested, but give it a try

who | awk '{ if (NR!=1 && NR!=2) {print} }' | sed -e 's/ /, /g'

Upvotes: 0

Related Questions