Flat file to csv conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Flat file to csv conversion
# 1  
Old 03-19-2010
Flat file to csv conversion

Hi Guy's can someone help me in converting the following

I have a flat text file which has several thousand lines which I need to convert to a csv it's got a consistent format but basically want every time it hit's txt to create a new line with the subsequent lines comma delimited for example

Code:
txt.1,line 1, line 2, line n
txt.2,line 1, line 2, line n
etc

The input file looks like this

Code:
txt.1
line 1
line 2
line n
txt.2
line 1
line 2
line n
txt.n
line n

Thanks All

Last edited by radoulov; 03-19-2010 at 12:02 PM.. Reason: Please use code tags!
# 2  
Old 03-19-2010
Tools Is this what you are looking for?

Code:
>cat p1.txt
txt.1
line 1
line 2
line n
txt.2
line 1
line 2
line n
txt.n
line n

>sed 's/txt/~txt/g' <p1.txt | tr "\n" "," | tr "~" "\n" | sed 's/,$//'

txt.1 ,line 1 ,line 2 ,line n
txt.2 ,line 1 ,line 2 ,line n
txt.n ,line n

# 3  
Old 03-19-2010
Perfect - thank you very much!!!!
# 4  
Old 03-19-2010
Or with awk:
Code:
awk '/txt/{if(s){print s}{s=$0;next}}{s=s "," $0}' file

# 5  
Old 03-19-2010
One last question - my fault for not being precise - what if the file name varies the other way ie

1.txt
2.txt

this produces slightly different result for sed, and awk is only providing one result for the file
# 6  
Old 03-19-2010
The given command should work, but to be more precise, if the lines are ending with txt you can use the $ anchor:
Code:
awk '/txt$/{if(s){print s}{s=$0;next}}{s=s "," $0}' file

More about anchors:

Regexp Operators - The GNU Awk User's Guide
# 7  
Old 03-19-2010
One in perl,

Code:
perl -lne '$\=""; $v =(/^txt/)?"\n$_":" $_"; print $v;' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Conversion of Binary csv file

Hello, I have a binary csv file that was created on 'Red Hat Enterprise Linux Server release 6.6'. Now we have transferred all files on Ubuntu 16.04.2 LTS/xenial On opening the file in Ubuntu, there are special characters ... (8 Replies)
Discussion started by: nans
8 Replies

2. UNIX for Dummies Questions & Answers

Convert flat file to csv

Hi I have a file like this: a=1 b=2 c=3 a=4 b=2 d=3 a=3 c=4 How can I change this to csv format a,b,c,d 1,2,3,, 4,2,,3 3,,4,, Please use code tags next time for your code and data. Thanks (10 Replies)
Discussion started by: sandip_2014
10 Replies

3. Shell Programming and Scripting

awk Flat File Conversion Script

In awk how would I flatten input.txt to output.txt? Input: givenname: Darth sn: Vadar mail: d.vadar@deathstar.com uid: dv12345 orclguid: 1234567890 givenname: Carlito sn: Brigante mail: c.brigante@paradise.com uid: cb12345 orclguid: 2134567890 Output: ... (3 Replies)
Discussion started by: u20sr
3 Replies

4. UNIX for Dummies Questions & Answers

Conversion of flat file to Mainframe file

Hi guys, I have a flat file created by ETL tool. Now the flat file has 4 columns. 1st column is a Character(17), 2nd Column Character(4), 3rd column is a decimal column and 4th column is also a decimal column. I have to convert this file into a Mainframe file in the following format. ... (4 Replies)
Discussion started by: mac4rfree
4 Replies

5. Shell Programming and Scripting

perl : number to date conversion in CSV file

I have a CSV file in the below format. while generating CSV file from excel sheet , date in excel sheet(Format :Mon 8/28/2012) got converted into the below format with numbers 41148,41149 so on. Could anyone please let me know how to the convert the numbers(41148,41149 so on.) to its actual... (2 Replies)
Discussion started by: giridhar276
2 Replies

6. Shell Programming and Scripting

reading a csv file and creating a flat file

hi i have written a script for reading a csv file and creating a flat file, suggest if this script can be optimized #---------------- FILENAME="$1" SCRIPT=$(basename $0) #-----------------------------------------// function usage { echo "\nUSAGE: $THIS_SCRIPT file_to_process\n"... (3 Replies)
Discussion started by: mprakasheee
3 Replies

7. Shell Programming and Scripting

conversion of spaces into CSV format file

INput file attached in thread : Column widths at 24,73,82,87,121 characters (sed 's/./,/24;s/./,/73;s/./,/81;s/./,/87;s/./,/121;s/ *, */,/g' fixedinputfile >output.csv ). The client wants instead of hard coding the column widths as they are not fixed .he has given the hint stating that ( ... (3 Replies)
Discussion started by: sreenath1037
3 Replies

8. Shell Programming and Scripting

Conversion of below Tabs Tex file into CSV format file : shell script needed

Request if some one could provide me shell script that converts the below "input file" to "CSV format file" given Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- ----- ---------------------------------... (7 Replies)
Discussion started by: sreenath1037
7 Replies

9. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies

10. UNIX for Advanced & Expert Users

Flat File Conversion Format

Hi all, I've a flat file in this format: = " Record 1 Field1 -> XXXX Field2 -> 9558 Field3 -> 55AA Record 2 Field1 -> YYYY Field2 -> 12345 Field3 -> aa23 " And i want to convert it to (4 Replies)
Discussion started by: Loobian
4 Replies
Login or Register to Ask a Question