need to save the space when converting to CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to save the space when converting to CSV file
# 1  
Old 03-02-2012
need to save the space when converting to CSV file

Hi,

I have a text file with the following format. Some of the fields are blank.

Code:
1234    3456    23  45464  327837283232    343434
      5654353    34  34343    3434345          434242

....

....

....


I need to convert this file to a CSV file, like
Code:
1234,    ,23,  ,454,64,3278,3728,3232,    ,343,434

Here is my code:

Code:
inputFile="test"
outputFile="test.csv"
 
while read line
do
    f1=`echo $line | cut -c1-4`
    f2=`echo $line | cut -c5-8`
    f3..
    f4..
    .. 
    ..
    echo "$f1,$f2,$f3....." >> $outputFile
done < $inputFile

but in the outputFile, all the blank are erased. Can anyone help me out here?

Thanks!!Smilie

Last edited by joeyg; 03-02-2012 at 11:41 AM.. Reason: Please wrap sample data and scripts with CodeTags
# 2  
Old 03-02-2012
Please verify the input data

Please verify the input data, and be sure to wrap with CodeTags to maintain data integrity - spaces, tabs, etc...

I applied CodeTags to your original sample, but this does not look like it mapped correctly.
# 3  
Old 03-02-2012
If these are fixed-width fields:

Code:
$ cat fsplit.awk

BEGIN {
        OFS=","
        # Big list of character ranges, split into A[1]="1-4", ...
        X=split("1-4,5-5,11-12,13-13,14-16,17-18,20-23,24-27,28-31,32-32,33-35,36-38", A, ",");
        for(N=1; N<=X; N++)
        {
                split(A[N], B, "-"); # Split "1-4" into B[1]="1", B[2]="4"
                delete A[N];
                S[N]=B[1]; # Keep the same
                E[N]=(B[2]-B[1])+1; # Calculate the length from the offset
        }
}

{
        STR=$0
        $0=""
        # Assemble a big list of fields from each given position, stripping
        # the appropriate section of string out of STR.
        for(N=1; N<=X; N++) $N=substr(STR, S[N], E[N]);

# print every line.
} 1

$ awk -f fsplit.awk data

1234, ,23, ,454,64,3278,3728,3232, ,343,434

$

I suspect the fields will be wrong for your data given the lack of code tags, but you can modify the numbers as you see fit. It's just
Code:
START1-END1,START2-END2,START3-END3,...

# 4  
Old 03-02-2012
Corona688, thank you for your code!! I also fixed my code to work. I need to change:
Code:
f1=`echo $line | cut -c1-4`

to:
Code:
f1=`echo "$line" | cut -c1-4`

Smilie

Last edited by Franklin52; 03-05-2012 at 03:10 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

3. Shell Programming and Scripting

Converting data for text file to csv

Gents Using the script attached (raw2csv). i use to create the file .csv.. The input file is called 201.raw. Kindly can you check if there is easy way to do it. The script works fine but takes a lot time to process Thanks for your help (8 Replies)
Discussion started by: jiam912
8 Replies

4. Shell Programming and Scripting

Converting rows to columns in csv file

Hi, I have a requirement to convert rows into columns. data looks like: c1,c2,c3,.. r1,r2,r3,.. p1,p2,p3,.. and so on.. output shud be like this: c1,r1,p1,.. c2,r2,p2,.. c3,r3,p3,.. Thanks in advance, (12 Replies)
Discussion started by: Divya1987
12 Replies

5. Shell Programming and Scripting

FILE_ID extraction from file name and save it in CSV file after looping through each folders

FILE_ID extraction from file name and save it in CSV file after looping through each folders My files are located in UNIX Server, i want to extract file_id and file_name from each file .and save it in a CSV file. How do I do that? I have folders in unix environment, directory structure is... (15 Replies)
Discussion started by: princetd001
15 Replies

6. Shell Programming and Scripting

Converting variable space width data into CSV data in bash

Hi All, I was wondering how I can convert each line in an input file where fields are separated by variable width spaces into a CSV file. Below is the scenario what I am looking for. My Input data in inputfile.txt 19 15657 15685 Sr2dReader 107.88 105.51... (4 Replies)
Discussion started by: vharsha
4 Replies

7. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

8. Shell Programming and Scripting

Converting txt file in csv

HI All, I have a text file memory.txt which has following values. Average: 822387 7346605 89.93 288845 4176593 2044589 51883 2.47 7600 i want to convert this file in csv format and i am using following command to do it. sed s/_/\./g <... (3 Replies)
Discussion started by: mkashif
3 Replies

9. Shell Programming and Scripting

Data fetched from text file and save in a csv file

Hi i have wriiten a script which fetches the data from text file, and saves in the output in a text file itself, but i want that the output should save in different columns. I have the output like: For Channel:response_time__24.txt 1547 data points 0.339 0.299 0.448 0.581 7.380 ... (1 Reply)
Discussion started by: rohitkalia
1 Replies
Login or Register to Ask a Question