Sponsored Content
Top Forums Shell Programming and Scripting Auto correct a csv file using UNIX shell script. Post 302889202 by wisecracker on Wednesday 19th of February 2014 10:34:14 AM
Old 02-19-2014
Quote:
Originally Posted by karthik_ak
I ran the
Code:
od -c filename

to check for carriage return characters. There are no carriage returns.
Oh yes there are 0x0D, 9 of them in fact...
From CygWin, (and the same on OSX 10.7.5, default bash terninal.)...
Code:
AMIGA:~> cd /tmp
AMIGA:/tmp> hexdump sample.txt
0000000 61 62 63 64 2c 37 38 39 37 35 36 2c 32 30 31 34
0000020 30 32 31 31 2c 32 30 31 34 30 32 30 32 2c 34 36
0000040 34 35 33 32 2e 30 30 2c 43 2c 58 5a 59 2c 2c 2c
0000060 53 65 74 74 6c 65 6d 65 6e 74 20 6f 66 20 70 75
0000100 72 63 68 61 73 65 2e 2c 2c 31 0d 0a 61 62 63 64
0000120 2c 37 39 35 31 35 36 2c 32 30 31 34 30 32 31 31
0000140 2c 32 30 31 34 30 32 30 32 2c 35 34 38 39 36 32
0000160 2e 35 32 2c 43 2c 4a 50 59 2c 2c 2c 50 61 79 65
0000200 6d 65 6e 74 20 74 6f 3a 20 74 6f 6b 79 6f 20 42
0000220 4e 46 3d 4e 2c 2c 31 0d 0a 61 62 63 64 2c 37 36
0000240 33 32 35 36 2c 32 30 31 34 30 32 31 31 2c 32 30
0000260 31 34 30 32 30 32 2c 34 32 31 38 38 2e 38 30 2c
0000300 43 2c 55 53 44 2c 2c 2c 72 65 63 69 65 76 65 64
0000320 20 66 72 6f 6d 20 2c 2c 31 0d 0a 61 62 63 64 2c
0000340 37 35 36 35 35 36 2c 32 30 31 34 30 32 31 31 2c
0000360 32 30 31 34 30 32 30 32 2c 34 35 36 38 39 38 2e
0000400 32 38 2c 43 2c 45 55 52 2c 2c 2c 50 61 79 6d 65
0000420 6e 74 20 76 69 61 2e 2c 2c 31 0d 0a 61 62 63 64
0000440 2c 37 33 32 36 35 36 2c 32 30 31 34 30 32 31 31
0000460 2c 32 30 31 34 30 32 30 32 0d 0a 2c 35 38 34 36
0000500 31 2e 31 30 2c 43 2c 49 4e 52 2c 2c 2c 53 65 74
0000520 74 6c 65 6d 65 6e 74 20 6f 66 20 70 75 72 63 68
0000540 61 73 65 2e 2c 2c 31 0d 0a 61 62 63 64 2c 37 39
0000560 38 37 35 36 2c 32 30 31 34 30 32 31 31 2c 32 30
0000600 31 34 30 32 30 32 2c 33 32 33 31 35 36 2e 38 32
0000620 2c 43 2c 41 55 44 2c 2c 2c 53 65 74 74 6c 65 6d
0000640 65 6e 74 20 6f 66 20 70 75 72 63 68 61 73 65 2e
0000660 2c 2c 31 0d 0a 61 62 63 64 2c 37 38 39 38 35 36
0000700 2c 32 30 31 34 30 32 31 31 2c 32 30 31 34 30 32
0000720 30 32 2c 38 34 36 32 31 2e 36 33 2c 43 2c 43 48
0000740 46 2c 2c 2c 53 65 74 74 6c 65 6d 65 6e 74 20 6f
0000760 66 20 73 61 6c 65 20 6e 6f 20 42 33 35 34 35 33
0001000 20 45 55 52 20 34 37 31 2e 36 30 2c 2c 31 0d 0a
0001020 61 62 63 64 2c 37 38 35 34 35 36 2c 32 30 31 34
0001040 30 32 31 31 2c 32 30 31 34 30 32 30 32 2c 38 36
0001060 34 32 31 2e 34 30 2c 43 2c 55 53 44 2c 2c 2c 53
0001100 65 74 74 6c 65 6d 65 6e 74 20 6f 66 20 70 75 72
0001120 63 68 61 73 65 2e 2c 2c 31 0d 0a
0001133
AMIGA:/tmp> dos2unix sample.txt
dos2unix: converting file sample.txt to Unix format ...
AMIGA:/tmp> ls -l sample*
-rwxr-xr-x 1 Tami None 594 Feb 19 15:25 sample.txt
-rwxr-xr-x 1 Tami None 603 Feb 19 15:23 sample1.txt
AMIGA:/tmp> _

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying a csv file from Shell Script

Hi all, I have some script that creates a temp csv file. What I need to do is do some search and replace and modify the file from my shell script. I know the commands to open the file and then apply the reg ex but wasnt sure how I could do this from a script and modify the file? Any help... (2 Replies)
Discussion started by: not4google
2 Replies

2. UNIX for Advanced & Expert Users

Unix Shell Script with output to CSV File

Hi, I have a unix shell script that is outputting results from an SQL query to a *.csv file, using utl_file.put_line. The resulting file is then sent out via e-mail as a mail attachment. The issue I have is that when the mailed attachment is opened in Excel the first column is shown as... (1 Reply)
Discussion started by: heather.morton@
1 Replies

3. Shell Programming and Scripting

how to create csv file using shell script

I have a file in multiple directory which has some records in the following format File: a/latest.txt , b/latest.txt, c/latest.txt -> Name=Jhon Age=27 Gender=M Street=LA Road Occupation=Service I want to generate a csv file from the above file as follows File: output.csv -> ... (9 Replies)
Discussion started by: rjk2504
9 Replies

4. Shell Programming and Scripting

Exporting data as a CSV file from Unix shell script

Friends...This is the first time i am trying the report generation using shell script... any suggestions are welcome. Is there a way to set the font size & color when i am exporting the data from unix shell script as a CSV file ? The following sample data is saved as a .csv file in the... (2 Replies)
Discussion started by: appu2176
2 Replies

5. UNIX for Dummies Questions & Answers

Help to parse csv file with shell script

Hello ! I am very aware that this is not the first time this question is asked here, because I have already read a lot of previous answers, but none of them worked, so... As said in the title, I want to read a csv file with a bash script. Here is a sample of the file: ... (4 Replies)
Discussion started by: Grhyll
4 Replies

6. Post Here to Contact Site Administrators and Moderators

Auto correct a csv file using UNIX shell script.

Hi All, There are list of 4-5 .csv files which has 12 columns.In some cases one of the record is split into 2 records. What needs to be done is this split record has to be auto corrected and placed in the csv file. Eg: Let us consider sample.csv file and in normal conditions the file... (1 Reply)
Discussion started by: karthik_ak
1 Replies

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

8. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

9. UNIX for Beginners Questions & Answers

How to zip csv files having specific pattern in a directory using UNIX shell script?

I have files in a Linux directory . Some of the file is listed below -rw-rw-r--. 1 roots roots 0 Dec 23 02:17 zzz_123_00000_A_1.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_121_00000_A_2.csv -rw-rw-r--. 1 roots roots 0 Dec 23 02:18 zzz_124_00000_A_3.csv drwxrwxr-x. 2 roots roots 6 Dec 23... (4 Replies)
Discussion started by: Balraj
4 Replies

10. Shell Programming and Scripting

How to auto correct a failing command?

If a command is not found, e.g. nawk, this is how I fix the problem ] && NAWK=/usr/bin/gawk ] && NAWK=/usr/bin/nawk ] && NAWK=/usr/bin/awkI use $NAWK an the set the appropriate value based on the system it runs. How can I implement a similar fix for a command found but illegal argument.... (6 Replies)
Discussion started by: mohtashims
6 Replies
csv(n)								  CSV processing							    csv(n)

__________________________________________________________________________________________________________________________________________________

NAME
csv - Procedures to handle CSV data. SYNOPSIS
package require Tcl 8.3 package require csv ?0.7.2? ::csv::iscomplete data ::csv::join values {sepChar ,} {delChar "} ::csv::joinlist values {sepChar ,} {delChar "} ::csv::joinmatrix matrix {sepChar ,} {delChar "} ::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none} ::csv::read2queue ?-alternate? chan q {sepChar ,} ::csv::report cmd matrix ?chan? ::csv::split ?-alternate? line {sepChar ,} {delChar "} ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none} ::csv::split2queue ?-alternate? q line {sepChar ,} ::csv::writematrix m chan {sepChar ,} {delChar "} ::csv::writequeue q chan {sepChar ,} {delChar "} _________________________________________________________________ DESCRIPTION
The csv package provides commands to manipulate information in CSV FORMAT (CSV = Comma Separated Values). COMMANDS
The following commands are available: ::csv::iscomplete data A predicate checking if the argument data is a complete csv record. The result is a boolean flag indicating the completeness of the data. The result is true if the data is complete. ::csv::join values {sepChar ,} {delChar "} Takes a list of values and returns a string in CSV format containing these values. The separator character can be defined by the caller, but this is optional. The default is ",". The quoting character can be defined by the caller, but this is optional. The default is '"'. ::csv::joinlist values {sepChar ,} {delChar "} Takes a list of lists of values and returns a string in CSV format containing these values. The separator character can be defined by the caller, but this is optional. The default is ",". The quoting character can be defined by the caller, but this is optional. The default is '"'. Each element of the outer list is considered a record, these are separated by newlines in the result. The ele- ments of each record are formatted as usual (via ::csv::join). ::csv::joinmatrix matrix {sepChar ,} {delChar "} Takes a matrix object following the API specified for the struct::matrix package and returns a string in CSV format containing these values. The separator character can be defined by the caller, but this is optional. The default is ",". The quoting character can be defined by the caller, but this is optional. The default is '"'. Each row of the matrix is considered a record, these are separated by newlines in the result. The elements of each record are formatted as usual (via ::csv::join). ::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none} A wrapper around ::csv::split2matrix (see below) reading CSV-formatted lines from the specified channel (until EOF) and adding them to the given matrix. For an explanation of the expand argument see ::csv::split2matrix. ::csv::read2queue ?-alternate? chan q {sepChar ,} A wrapper around ::csv::split2queue (see below) reading CSV-formatted lines from the specified channel (until EOF) and adding them to the given queue. ::csv::report cmd matrix ?chan? A report command which can be used by the matrix methods format 2string and format 2chan. For the latter this command delegates the work to ::csv::writematrix. cmd is expected to be either printmatrix or printmatrix2channel. The channel argument, chan, has to be present for the latter and must not be present for the first. ::csv::split ?-alternate? line {sepChar ,} {delChar "} converts a line in CSV format into a list of the values contained in the line. The character used to separate the values from each other can be defined by the caller, via sepChar, but this is optional. The default is ",". The quoting character can be defined by the caller, but this is optional. The default is '"'. If the option -alternate is specified a slightly different syntax is used to parse the input. This syntax is explained below, in the section FORMAT. ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none} The same as ::csv::split, but appends the resulting list as a new row to the matrix m, using the method add row. The expansion mode specified via expand determines how the command handles a matrix with less columns than contained in line. The allowed modes are: none This is the default mode. In this mode it is the responsibility of the caller to ensure that the matrix has enough columns to contain the full line. If there are not enough columns the list of values is silently truncated at the end to fit. empty In this mode the command expands an empty matrix to hold all columns of the specified line, but goes no further. The overall effect is that the first of a series of lines determines the number of columns in the matrix and all following lines are truncated to that size, as if mode none was set. auto In this mode the command expands the matrix as needed to hold all columns contained in line. The overall effect is that after adding a series of lines the matrix will have enough columns to hold all columns of the longest line encountered so far. ::csv::split2queue ?-alternate? q line {sepChar ,} The same as ::csv::split, but appending the resulting list as a single item to the queue q, using the method put. ::csv::writematrix m chan {sepChar ,} {delChar "} A wrapper around ::csv::join taking all rows in the matrix m and writing them CSV formatted into the channel chan. ::csv::writequeue q chan {sepChar ,} {delChar "} A wrapper around ::csv::join taking all items in the queue q (assumes that they are lists) and writing them CSV formatted into the channel chan. FORMAT
The format of regular CSV files is specified as [1] Each record of a csv file (comma-separated values, as exported e.g. by Excel) is a set of ASCII values separated by ",". For other languages it may be ";" however, although this is not important for this case as the functions provided here allow any separator character. [2] If and only if a value contains itself the separator ",", then it (the value) has to be put between "". If the value does not con- tain the separator character then quoting is optional. [3] If a value contains the character ", that character is represented by "". [4] The output string "" represents the value ". In other words, it is assumed that it was created through rule 3, and only this rule, i.e. that the value was not quoted. An alternate format definition mainly used by MS products specifies that the output string "" is a representation of the empty string. In other words, it is assumed that the output was generated out of the empty string by quoting it (i.e. rule 2), and not through rule 3. This is the only difference between the regular and the alternate format. The alternate format is activated through specification of the option -alternate to the various split commands. EXAMPLE
Using the regular format the record 123,"123,521.2","Mary says ""Hello, I am Mary""","" is parsed into the items a) 123 b) 123,521.2 c) Mary says "Hello, I am Mary" d) (the empty string) Using the alternate format the result is a) 123 b) 123,521.2 c) Mary says "Hello, I am Mary" d) " instead. As can be seen only item (d) is different, now a " instead of the empty string. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category csv of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
matrix, queue KEYWORDS
csv, matrix, package, queue, tcllib CATEGORY
Text processing COPYRIGHT
Copyright (c) 2002-2009 Andreas Kupries <andreas_kupries@users.sourceforge.net> csv 0.7.2 csv(n)
All times are GMT -4. The time now is 10:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy