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.
# 22  
Old 02-18-2014
Now that we know what your problem really is, let's go back to Scrutinizer's approach in message #2 in this thread and extend it to get rid of carriage returns in your input file and add them back in your output after split lines are joined:
Code:
awk '{  gsub(/\r/, "")          # get rid of existing carriage returns
}
NF<12 { getline p               # join split lines
        $0 = $0 p
}       
{       printf("%s\r\n", $0)    # print joined lines with CR/NL terminators
}' FS=, OFS=, sample.csv

This seems to do what you want for lines ending in a comma and for lines starting with a comma. And, it works correctly even if the first field on a line or the last field on a line is an empty string.
# 23  
Old 02-18-2014
Small modification to Don's suggestion and move the gsub down, so that p also get stripped of \r before it gets added back in at end...
Code:
awk '
  NF<12 {
    getline p               # join split lines
    $0 = $0 p
  }
  {
    gsub(/\r/, "")          # get rid of existing carriage returns
    printf("%s\r\n", $0)    # print joined lines with CR/NL terminators
  }
' FS=, OFS=, sample.csv

This User Gave Thanks to Scrutinizer For This Post:
# 24  
Old 02-19-2014
The code by Don removes the entire line before and after the first broken line.

The Code by Scrutinizer aslo does the same and each line repeats itself.
# 25  
Old 02-19-2014
Use Scrutinizer's code; not mine.

Neither of our scripts do what you describe with the data you have shown us (with or without carriage returns in the file).

Please upload (rather than copy and paste) sample data that makes Scrutinizer's code behave the way you have described it. The only reason I can see for a line to repeat itself is if your last line (or, if the last line was split, last two lines) does not have 12 fields (i.e., 11 commas).
# 26  
Old 02-19-2014
Hi, Karthik Ak:
Do you mind to use a shell script, just for a try.
Code:
#!/bin/ksh
tr -d '\r' <$1>$1.tmp
i=0
while read -r line
do
length=`echo "$line"|sed 's/[^,]//g'`
i=`expr $i + ${#length}`
if [ $i -eq 11 ];then
printf "$line\n"
i=0
else
printf "$line"
fi
done<$1.tmp
rm $1.tmp

Usage
Code:
./test.sh sample.csv

If you use bash instead of ksh, please modify the 1st line to
Code:
#!/bin/bash

# 27  
Old 02-19-2014
I have attached the file.please rename the extension to csv.
I wasnt able to upload a csv file.
# 28  
Old 02-19-2014
@lucas

That didnt work as well!
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