Linux command for exporting output to a csv file in separated columns
35,559
Solution 1
You only need to replace the tab with comas and set the output to a csv file:
grep "hello world" | tr "\\t" "," > file.csv
the 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
Solution 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.
Related videos on Youtube
Author by
hakuro
Updated on September 18, 2022Comments
-
hakuro almost 2 years
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:
-
Scott - Слава Україні over 5 years(1) As far as I can tell, your question has nothing to do with
grep
. You have some text (which happens to be a partial output fromlast
; i.e., produced bylast | grep
) and you want to do something with it. (2) What do you want to do? From the title and body of the question, it seems that you want to convert your text to a CSV format. (Show us an example of what you want.) From the image and the tags, it appears that you want to import your data into Excel. (Excel’s “Text to Columns” tool does this. Have you tried it? Why isn’t it good enough for you?) … (Cont’d) -
Scott - Слава Україні over 5 years(Cont’d) … (3) Oh, and, if you want to strip out a column, you should say so. (But, also, this is trivially done while loading the data into Excel, or after you have loaded it into Excel.) … … … … … … … … … … … … … … … … Please do not respond in comments; edit your question to make it clearer and more complete.
-
-
hakuro almost 7 yearstr " " "," > file.csv --> This part works. This is exactly what I've been looking for. Thank you so much.
-
Genaro Morales almost 7 yearsYou're welcome, if this answered your question mark it as an answer to help other to know that is useful. Regards.
-
dman79 almost 6 yearsWelcome to Super User. It's often helpful to offer a little more explanation with each answer, would you be able to provide further detail on what that command does, and how it solves the problem?
-
Dhimant Thanki almost 6 yearsHi zeel, This command will grab out the only columns that you need from the "last" command and will pour it into the excel file.