LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Create a Csv file (https://www.linuxquestions.org/questions/programming-9/create-a-csv-file-4175578396/)

imkornhulio 04-26-2016 02:09 PM

Create a Csv file
 
How do i create a Csv file from the list of file in the folder.

Particular folder has lot of pdf files

78945.pdf
12305.pdf

Csv file should look something like this

Column1,Column2
78945.pdf,78945
12305.pdf,12305

Can someone point me in the right direction. I know in Shell i have to use ls command and export it to csv but not sure how to get the second column.

Thanks

thesnow 04-26-2016 02:20 PM

You could do something like this:

Code:

for i in `ls *.pdf`; do echo $i,${i%.pdf}; done > list.csv

imkornhulio 04-26-2016 02:28 PM

Thank you. how do i add the First Line with Column Names ?

TB0ne 04-26-2016 02:34 PM

Quote:

Originally Posted by imkornhulio (Post 5536947)
how do i add the First Line with Column Names ?

Really?? You've been asking about such things since 2008....after EIGHT YEARS, have you not progressed ANY with your skills, and are you not able to apply anything you've been told previously?
http://www.linuxquestions.org/questi...v-file-663835/
http://www.linuxquestions.org/questi...to-tab-683201/
http://www.linuxquestions.org/questi...t-file-699125/
http://www.linuxquestions.org/questi...v-file-898955/

imkornhulio 04-26-2016 03:19 PM

sed -i '1icolumn1,column2' list.csv found this on web.

grail 04-27-2016 10:37 AM

Assuming a script, just echo those lines in first. Also, I would not use ls in case of word splitting and use a simple glob (ie. for f in *)

ntubski 04-27-2016 12:25 PM

Quote:

Originally Posted by grail
Assuming a script, just echo those lines in first. Also, I would not use ls in case of word splitting and use a simple glob (ie. for f in *)

Yup:
Code:

{ echo 'Column1,Column2'; for i in *.pdf; do echo "$i,${i%.pdf}"; done; } > list.csv


All times are GMT -5. The time now is 04:18 PM.