How to convert space&tab delimited file to CSV?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert space&tab delimited file to CSV?
# 1  
Old 07-22-2014
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.

Code:
# 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   HP        Ultrium 6-SCSI      J3LZ    50:03:08:c0:02:72:c0:97  F00272C097               0/0/6/1/1.145.17.255.4.0.0   /dev/rmt/c103t0d0BEST
/dev/rmt/tsmmt48   IBM       ULT3580-TD3         5AT0                             5458623002               0/0/6/1/1.145.0.255.8.0.0    /dev/rmt/c72t0d0BESTn
/dev/rmt/tsmmt53   IBM       ULT3580-TD3         5AT0                             5458623006               0/0/6/1/1.145.0.255.8.0.1    /dev/rmt/c72t0d1BESTn

I tried the below

Code:
sed 's/\t/ /g' test.txt |sed 's/  */ /g' |sed 's/ /,/g' >test.csv

It works but unfortunately, few fields have blank values and with above technique, values are getting shifted to left. Please advise.

Also, how to find out if a file has spaces or tabs, do I need to use an editor which shows control characters?

Thanks in advance!
# 2  
Old 07-22-2014
Perhaps you could use the expand command and then replace 2 or more spaces with comma:
Code:
# expand text.txt | sed 's/   */,/g' > test.csv

or replace tab and 2 or more spaces with comma:

Code:
# sed -e 's/\t/,/g' -e 's/   */,/g' infile

# 3  
Old 07-22-2014
Thanks Chubler_XL for the reply but unfortunately both solutions generate same output as mine.
# 4  
Old 07-22-2014
Can you post your input and the (undesired) output you are getting.
# 5  
Old 07-22-2014
Quote:
Originally Posted by Chubler_XL
Can you post your input and the (undesired) output you are getting.
Code:
$ 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   HP        Ultrium 6-SCSI      J3LZ    50:03:08:c0:02:72:c0:97  F00272C097               0/0/6/1/1.145.17.255.4.0.0   /dev/rmt/c103t0d0BEST
/dev/rmt/tsmmt48   IBM       ULT3580-TD3         5AT0                             5458623002               0/0/6/1/1.145.0.255.8.0.0    /dev/rmt/c72t0d0BESTn
/dev/rmt/tsmmt53   IBM       ULT3580-TD3         5AT0                             5458623006               0/0/6/1/1.145.0.255.8.0.1    /dev/rmt/c72t0d1BESTn


$ expand test.txt | sed 's/   */,/g'
/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,HP,Ultrium 6-SCSI,J3LZ,50:03:08:c0:02:72:c0:97,F00272C097,0/0/6/1/1.145.17.255.4.0.0,/dev/rmt/c103t0d0BEST
/dev/rmt/tsmmt48,IBM,ULT3580-TD3,5AT0,5458623002,0/0/6/1/1.145.0.255.8.0.0,/dev/rmt/c72t0d0BESTn
/dev/rmt/tsmmt53,IBM,ULT3580-TD3,5AT0,5458623006,0/0/6/1/1.145.0.255.8.0.1,/dev/rmt/c72t0d1BESTn


$ sed -e 's/\t/,/g' -e 's/   */,/g' 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,HP,Ultrium 6-SCSI,J3LZ,50:03:08:c0:02:72:c0:97,F00272C097,0/0/6/1/1.145.17.255.4.0.0,/dev/rmt/c103t0d0BEST
/dev/rmt/tsmmt48,IBM,ULT3580-TD3,5AT0,5458623002,0/0/6/1/1.145.0.255.8.0.0,/dev/rmt/c72t0d0BESTn
/dev/rmt/tsmmt53,IBM,ULT3580-TD3,5AT0,5458623006,0/0/6/1/1.145.0.255.8.0.1,/dev/rmt/c72t0d1BESTn


$


Desired output
Code:
/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,HP,Ultrium 6-SCSI,J3LZ,50:03:08:c0:02:72:c0:97,F00272C097,0/0/6/1/1.145.17.255.4.0.0,/dev/rmt/c103t0d0BEST
/dev/rmt/tsmmt48,IBM,ULT3580-TD3,5AT0,,5458623002,0/0/6/1/1.145.0.255.8.0.0,/dev/rmt/c72t0d0BESTn
/dev/rmt/tsmmt53,IBM,ULT3580-TD3,5AT0,,5458623006,0/0/6/1/1.145.0.255.8.0.1,/dev/rmt/c72t0d1BESTn

# 6  
Old 07-22-2014
You may have to deal with missing data individually, here I check the 58th character to see if it is a space and replace all spaces from here with a comma. You will need to repeat this technique for any column that may contain blank values.

Code:
sed -Ee 's/^(.{57})  */\1,/' -e 's/\t/,/g' -e 's/   */,/g' infile

# 7  
Old 07-23-2014
Hi, maybe the best approach is to start with how the data was generated in the first place?
How are you generating the data thats going into this file? If its been giving to you by some one else, it may be a good idea to ask them this question.

Maybe there is a a way to control the input. Those blank spaces can be populated with "null" or "blank".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output file with <Tab> or <Space> Delimited

Input file: xyz,pqrs.lmno,NA,NA,NA,NA,NA,NA,NA abcd,pqrs.xyz,NA,NA,NA,NA,NA,NA,NA Expected Output: xyz pqrs.lmno NA NA NA NA NA NA NA abcd pqrs.xyz NA NA NA NA NA NA NA Command Tried so far: awk -F"," 'BEGIN{OFS=" ";} {print}' $File_Path/File_Name.csv Issue:... (5 Replies)
Discussion started by: TechGyaann
5 Replies

2. 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

3. 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

4. Shell Programming and Scripting

How to make tab delimited file to space delimited?

Hi How to make tab delimited file to space delimited? in put file: ABC kgy jkh ghj ash kjl o/p file: ABC kgy jkh ghj ash kjl Use code tags, thanks. (1 Reply)
Discussion started by: jagdishrout
1 Replies

5. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

6. 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

7. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

8. 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

9. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: dtidke
5 Replies

10. UNIX for Dummies Questions & Answers

Converting Space delimited file to Tab delimited file

Hi all, I have a file with single white space delimited values, I want to convert them to a tab delimited file. I tried sed, tr ... but nothing is working. Thanks, Rajeevan D (16 Replies)
Discussion started by: jeevs81
16 Replies
Login or Register to Ask a Question