Linux command for exporting output to a csv file in separated columns
Andrew Mclaughlin
I am wondering when using Linux command line, how can I output a result i grep to a CSV file in different columns.
Here are the columns i grep from 'last' command
The final format, I would like to achieve, right now I have to import the data manually from a txt file is the following:
23 Answers
You only need to replace the tab with comas and set the output to a csv file:
grep "hello world" | tr "\\t" "," > file.csvthe text in grep can be a cat to your file to get all the content
I'm not sure if it is a tab or only a white space on your output, in case there is a white space:
grep "hello world" | tr " " "," > file.csv 2 Another way is to sed the output with
df -h | sed -E 's/ +/,/g'In my case I needed to grab the df output.
The Plus-sign is a multiplicator (one or more of in this case), and the substitution is ,. The -E must be used, because we want to use regex.
last | awk '{print $3,$4,$5,$6,$8,$9}' > gift.xls
This command is perfect for your requirements.
2