Auto correct a csv file using UNIX shell script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Auto correct a csv file using UNIX shell script.
# 29  
Old 02-19-2014
Scrutinizer's script produced exactly the output I expected. No lines were removed (other than 2 partial lines were joined into the desired single line). And, no line was repeated.

I guess that leaves one other obvious possible mistake. When you copied Scrutinizer's awk script onto your system, did you accidentally add carriage return characters into the script itself? (Use od -c scriptname and look for \r in the output.) If so, you need to get rid of them. If we assume that you had named the script joiner.sh, try the following commands:
Code:
cp joiner.sh _joiner.sh             # save the current script
tr -d '\r' < _joiner.sh > joiner.sh # remove carriage returns from the script

and then try running the script again.
# 30  
Old 02-19-2014
I ran the
Code:
od -c filename

to check for carriage return characters. There are no carriage returns.

---------- Post updated at 01:48 PM ---------- Previous update was at 01:46 PM ----------

Code:
 
perl -pi.orig -e 's/,\r+\n//g;s/\n,/,/g' infile

Can we modify the above code to work in place where the broken line is starting from a comma?

Last edited by karthik_ak; 02-19-2014 at 04:19 AM.. Reason: Enclosed the code.
# 31  
Old 02-19-2014
Hi, I modified your code and test, hope can help you
Code:
perl -pi.orig -e '@a=$_=~m/,/g;$a=@a;if($b!=1){if($a<11){s/\r+\n//};$b=1}else{$b=0}' sample.txt

# 32  
Old 02-19-2014
This is the result of the the modified code

Code:
 
abcd,789756,20140211,20140202,464532.00,C,XZY,,,Settlement of purchase.,,1
abcd,795156,20140211,20140202,548962.52,C,JPY,,,Payement to: tokyo BNF=N,,1
abcd,763256,20140211,20140202,42188.80,C,USD,,,recieved from ,,1
abcd,756556,20140211,20140202,456898.28,C,EUR,,,Payment via.,,1abcd,732656,20140211,20140202
,58461.10,C,INR,,,Settlement of purchase.,,1abcd,798756,20140211,20140202,323156.82,C,AUD,,,Settlement of purchase.,,1
abcd,789856,20140211,20140202,84621.63,C,CHF,,,Settlement of sale no B35453 EUR 471.60,,1
abcd,785456,20140211,20140202,86421.40,C,USD,,,Settlement of purchase.,,1

---------- Post updated at 06:19 PM ---------- Previous update was at 02:47 PM ----------

The below code works for all but it deletes the very first line.

Code:
 
perl -pi.orig -e '$/=undef; $_=<>; s/,[\r\n]+/,/g;s/[\r\n]+,/,/g;' sample.csv

# 33  
Old 02-19-2014
Hi, looks like your environment is so different with me.
For your code, I find that once $_=<> executed, the very first line would be deleted.
So may you try this?
Code:
perl -pi.orig -e '$/=undef; s/,[\r\n]+/,/g;s/[\r\n]+,/,/g' sample.csv

This User Gave Thanks to Lucas_0418 For This Post:
# 34  
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> _

# 35  
Old 02-19-2014
Another stab in the dark:
Code:
awk "NF<12 { getline p               # join split lines
        \$0 = \$0 p
}       
{  gsub(/\r/, x); printf(\"%s\r\n\", \$0)    # print joined lines with CR/NL terminators
}" FS=, OFS=, sample.csv

--
I think we still do not have a clear picture of what you are running on. In #9 you say you are running on Unix, in #11 you are running on Windows. Could you shed some light? Are you using Cygwin?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question