Shell scripting:from text file to CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scripting:from text file to CSV
# 1  
Old 06-18-2013
Shell scripting:from text file to CSV

Hello friends,
I have a file as follows:

Code:
"empty line" 
content1 
content2 
content3

content1 
content2 
content3  

content1 
content2 
content3



It starts with an empty line
,

how can i get a csv like this:

Code:
content1,content2,content3 
content1,content2,content3 
content1,content2,content3

many thanks for your attention and big help.
# 2  
Old 06-18-2013
Code:
awk -F"\n" 'BEGIN{RS=""; OFS=",";} {print $1,$2,$3}' file

This User Gave Thanks to rajamadhavan For This Post:
# 3  
Old 06-18-2013
This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-18-2013
Another approach:
Code:
xargs -n3 < file | tr ' ' ','

This User Gave Thanks to Yoda For This Post:
# 5  
Old 06-18-2013
Many thanks,

Code:
awk -F"\n" 'BEGIN{RS=""; OFS=",";} {print $1,$2,$3}' file

please, can you explain a bit your code, iŽd like to learn how to use AWK for this kind of tasks!

---------- Post updated at 10:11 AM ---------- Previous update was at 10:07 AM ----------

For Yoda, your code doesn't works, anyway many thanks!

Last edited by kraterions; 06-18-2013 at 12:10 PM.. Reason: code
# 6  
Old 06-18-2013
Try also:
Code:
awk  '{$1=$1}1' RS="" OFS="," file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-18-2013
Quote:
Originally Posted by kraterions
For Yoda, your code doesn't works, anyway many thanks!
I don't know why, but it works for me:
Code:
$ cat file

content1
content2
content3

content1
content2
content3

content1
content2
content3

Code:
$ xargs -n3 < file | tr ' ' ','
content1,content2,content3
content1,content2,content3
content1,content2,content3

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

2. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

3. Shell Programming and Scripting

Need a piece of shell scripting to remove column from a csv file

Hi, I need to remove first column from a csv file and i can do this by using below command. cut -f1 -d, --complement Mytest.csv I need to implement this in shell scripting, Whenever i am using the above command alone in command line it is working fine. I have 5 files in my directory and... (3 Replies)
Discussion started by: Samah
3 Replies

4. Shell Programming and Scripting

How to calculate avg values of csv file using shell scripting .?

hi all i have a reporting work and i want it to be automated using shell scripting kindly let me know how can i make that possibe . eg data are :... (2 Replies)
Discussion started by: Avinash shaw
2 Replies

5. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

6. Solaris

XML to Text file Parsing Using shell scripting

Hi, I want to parse an XML File using Shell Script preferably by using awk command, I/P file is : <gn:ExternalGsmCell id="016P3A"> <gn:attributes> <gn:mnc>410</gn:mnc> <gn:mcc>310</gn:mcc> <gn:lac>8016</gn:lac> ... (2 Replies)
Discussion started by: tech_frk
2 Replies

7. Shell Programming and Scripting

XML to Text file Parsing Using shell scripting

Hi folks, Need some help with XML to text file parsing , the following is the content of the XML File. <xn:SubNetwork id="SNJNPRZDCR0R03"> <xn:MeContext id="PRSJU0005"> <xn:VsDataContainer id="PRSJU0005"> <xn:attributes> ... (6 Replies)
Discussion started by: tech_frk
6 Replies

8. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

9. Shell Programming and Scripting

exporting number into .csv file in text form (no other extra charc) from shell script

I have written a k shell program which is executing a sql and exporting data in numeric form like 0412323444 into .csv file. the problem i am facing is that , the data is coming in excel formatted in scientific form like 4.1+E08,while my requirement is to store data as such 0412323444 in excel ( no... (5 Replies)
Discussion started by: Deepak_Rastogi
5 Replies

10. UNIX for Dummies Questions & Answers

Shell scripting adding text to top of file

Hi this is quite simple i am sure but without using awk or sed i need to add text to the top of a file this is what i have got so far #!bin/bash echo "Add text to top of file" read line echo $line >> file1 This adds the text to the bottom of the file can some1 please help cheers (7 Replies)
Discussion started by: meadhere
7 Replies
Login or Register to Ask a Question