How to convert tab delimited file to .csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert tab delimited file to .csv file
# 1  
Old 01-09-2009
How to convert tab delimited file to .csv file

Hi,

Can any one please help me in converting a tab delimited file in .csv file.

Records in my file are similar to mentioned below:

DET 001 0201 AC032508970 01478E1X8
DET 002 0202 AC032508971 01478E1X8

Could any one please suggest me what approach would be more suitable for this or if there is any direct method to convert flat file to .csv file.

Thanks and Regards,
Durwas
# 2  
Old 01-09-2009
using awk

That's pretty easy. Awk would seem a likely candidate, but printing double-quotes in older AWKs can be difficult. If that's a problem for you, let us know.
Code:
awk -F'\t' '{ for (n=1; n <= NF; ++n) { if (n>1) printf(","); printf("\"%s\"",$n); } print ""; }'

Since this also quotes numbers, some of the columns might need to be converted to numeric. But generally, that's not the case.
# 3  
Old 01-09-2009
as usual, tr is your friend:

tr ' ' ';' < inputfile > outpufile

..if you want to check first

cat inputfile | tr ' ' ';'

..and if the file is actually tab deliminates use

tr '\t' ';' < inputfile > outputfile
# 4  
Old 01-09-2009
Quote:
Originally Posted by arcadia
as usual, tr is your friend:
Except that he needs commas, not semi-colons. Also, fields with commas will need to be double-quoted. It occurred to me that a much simpler version can be given with sed:
Code:
sed -e 's/^/"/; s/$/"/; s/\t/","/g;'

# 5  
Old 04-07-2009
converting csv files to semicolons separated values. Bash script

The following script using awk fails in the first line of each csv file, what can i do to fix the problem?
#!/bin/bash
# list-glob.sh: Generating [list] in a for-loop, using "globbing"

echo

for file in *
# ^ Bash performs filename expansion
#+ on expressions that globbing recognizes.
do
ls -l "$file" # Lists all files in $PWD (current directory).
# Recall that the wild card character "*" matches every filename,
#+ however, in "globbing," it doesn't match dot-files.

# If the pattern matches no file, it is expanded to itself.
# To prevent this, set the nullglob option
#+ (shopt -s nullglob).
# Thanks, S.C.
done

echo; echo

for file in *xyz
do
awk -v dir=$PWD 'FS = "," {print NR";", $1";", $2";",$3";", FILENAME";", dir }' "$file" > $file.csv # Creates csv files in $PWD.
echo "Created file \"$file.csv\"".
done

echo

exit 0
# 6  
Old 04-07-2009
Quote:
Originally Posted by puertochico
The following script using awk fails in the first line of each csv file, what can i do to fix the problem?
#!/bin/bash
# list-glob.sh: Generating [list] in a for-loop, using "globbing"

echo

for file in *
# ^ Bash performs filename expansion
#+ on expressions that globbing recognizes.
do
ls -l "$file" # Lists all files in $PWD (current directory).
# Recall that the wild card character "*" matches every filename,
#+ however, in "globbing," it doesn't match dot-files.

# If the pattern matches no file, it is expanded to itself.
# To prevent this, set the nullglob option
#+ (shopt -s nullglob).
# Thanks, S.C.
done

echo; echo

for file in *xyz
do
awk -v dir=$PWD 'FS = "," {print NR";", $1";", $2";",$3";", FILENAME";", dir }' "$file" > $file.csv # Creates csv files in $PWD.
echo "Created file \"$file.csv\"".
done

echo

exit 0
Sorry
I got it
awk -v dir=$PWD 'BEGIN { FS="," } {print NR";", $1";", $2";",$3";", FILENAME";", dir }'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. Shell Programming and Scripting

Convert pipe demilited file to vertical tab delimited

Hi All, How can we convert pipe delimited ( or comma ) file to vertical tab (VT) delimited. Regards PK (4 Replies)
Discussion started by: prasson_ibm
4 Replies

3. UNIX for Beginners Questions & Answers

Convert Excel File (xls) to tab delimited text file on AIX

Hi i have a problem in my job i try to convert an excel file (xls extention) to text file (tab delimited), but no result with this comand cat xxx.xls > xxx.txt Do you have eny idea? PS: sorry for my english Thanks!! (4 Replies)
Discussion started by: frisso
4 Replies

4. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

5. Shell Programming and Scripting

How to convert space&tab delimited file to CSV?

Hello, I have a text file with space and tab (mixed) delimited file and need to convert into CSV. # cat test.txt /dev/rmt/tsmmt32 HP Ultrium 6-SCSI J3LZ 50:03:08:c0:02:72:c0:b5 F00272C0B5 0/0/6/1/1.145.17.255.0.0.0 /dev/rmt/c102t0d0BEST /dev/rmt/tsmmt37 ... (6 Replies)
Discussion started by: prvnrk
6 Replies

6. Shell Programming and Scripting

Convert a 3 column tab delimited file to a matrix

Hi all, I have a 3 columns input file like this: CPLX9PC-4943 CPLX9PC-4943 1 CPLX9PC-4943 CpxID123 0 CPLX9PC-4943 CpxID126 0 CPLX9PC-4943 CPLX9PC-5763 0.5 CPLX9PC-4943 CpxID13 0 CPLX9PC-4943 CPLX9PC-6163 0 CPLX9PC-4943 CPLX9PC-6164 0.04... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

7. Shell Programming and Scripting

how to convert comma delimited file to tab separator

Hi all, How can i convert comma delimited .csv file to tab separate using sed command or script. Thanks, Krupa (4 Replies)
Discussion started by: krupasindhu18
4 Replies

8. Shell Programming and Scripting

Convert CSV file (with double quoted strings) to pipe delimited file

Hi, could some help me convert CSV file (with double quoted strings) to pipe delimited file: here you go with the same data: 1,Friends,"$3.99 per 1,000 listings",8158here " 1,000 listings " should be a single field. Thanks, Ram (8 Replies)
Discussion started by: Ram.Math
8 Replies

9. UNIX for Dummies Questions & Answers

How to convert a text file into tab delimited format?

I have a text file that made using text editor in Ubuntu. However the text file is not being recognized as space or tab delimited, the formatting seems to be messed up. How can I convert the text file into tab delimited format? (3 Replies)
Discussion started by: evelibertine
3 Replies

10. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies
Login or Register to Ask a Question