Remove line breaks in csv file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove line breaks in csv file using shell script
# 1  
Old 06-14-2012
Remove line breaks in csv file using shell script

Hi All,

I've a csv file in which the record is getting break into 1 line or more than one line. I want to combine those splits into one line and remove the unwanted character existing in the record i.e. double quote symbol ("). The line gets break only when the record contains double quote(") symbol. Please find the following current and expected csv file format.

Current CSV file:
Code:
TESTID1|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (IC)|TIER1
TESTID2|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (MCA)|TIER2
TESTID3|ACTIVE|Todd|Geller|"Marketing Dir-Sales
 Model (MCA)"|TIER4
TESTID3|ACTIVE|Todd|Geller|"Marketing Dir
 
         -Sales
 
 Model (MCA)"|TIER4

Expected CSV file:
Code:
TESTID1|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (IC)|TIER1
TESTID2|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (MCA)|TIER2
TESTID3|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (MCA)|TIER4
TESTID3|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (MCA)|TIER4

I know its bit difficult to find the solution but it would be great if anyone can help me out in getting the script prepared to fix this issue.

Thanks in Advance.

Last edited by Franklin52; 06-14-2012 at 08:05 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-14-2012
Code:
$ awk 'ORS=!/\|TIER[0-9]*$/?FS:RS' infile | sed 's/\"//g;s/  //g'
TESTID1|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (IC)|TIER1
TESTID2|ACTIVE|Todd|Geller|Marketing Dir-Sales Model (MCA)|TIER2
TESTID3|ACTIVE|Todd|Geller|Marketing Dir-SalesModel (MCA)|TIER4
TESTID3|ACTIVE|Todd|Geller|Marketing Dir-SalesModel (MCA)|TIER4

# 3  
Old 06-14-2012
Try this (not tested):
Code:
awk -F"|" '{while(NF < 6){s=$0;getline;gsub(/^[ \t]+/,x); $0=s $0; gsub(q,x)}}1' q=\' file

# 4  
Old 06-14-2012
Thank you cabrao and Franklin for your responses.

I tried the following code but didn't work. Please suggest.
Code:
#!/bin/bash
awk -F"|" '{while(NF < 6){s=$0;getline;gsub(/^[ \t]+/,x); $0=s $0; gsub(q,x)}}1' q=\' ISM_FEED_20120607.csv >> test.csv

Please suggest.

Last edited by Franklin52; 06-14-2012 at 10:18 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 06-14-2012
Quote:
Originally Posted by rajak.net
Thank you cabrao and Franklin for your responses.

I tried the following code but didn't work. Please suggest.
Code:
#!/bin/bash
awk -F"|" '{while(NF < 6){s=$0;getline;gsub(/^[ \t]+/,x); $0=s $0; gsub(q,x)}}1' q=\' ISM_FEED_20120607.csv >> test.csv

Please suggest.
I can't with the info above...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove single-line breaks only in document

Regarding copy/pasted text of copyright-free book from archive.org (link below), in attempt to expand single-line-break paragraph text (not section headings or paragraph breaks) to wider right margin, Justify or Wrap in LIbreOffice is not working, and Find/Replace the paragraph mark ($) wraps all... (2 Replies)
Discussion started by: p1ne
2 Replies

2. UNIX for Beginners Questions & Answers

Remove line breaks and extra spaces

Hi, I want to remove all extra spaces, line breaks . Need a new line entry only for term starting"array" For eg: my input is array(), array(), array(), and my expected output is array(), array(), array(), Is it possible using awk? (5 Replies)
Discussion started by: rsi.245
5 Replies

3. Shell Programming and Scripting

Shell script that should remove unnecessary commas between double quotes in CSV file

i have data as below 123,"paul phiri",paul@yahoo.com,"po.box 23, BT","Eco Bank,Blantyre,Malawi" i need an output to be 123,"paul phiri",paul@yahoo.com,"po.box 23 BT","Eco Bank Blantyre Malawi" (5 Replies)
Discussion started by: mathias23
5 Replies

4. Shell Programming and Scripting

Remove Space and blank line from file in UNIX shell script

I have below file. I want to remove space at begining of every line and then after also remove blank line from file. I use below code for each operation. sed -e 's/^*//' < check.txt > check1.txt sed '/^\s*$/d' < check1.txt > check2.txt above code not remove all the space... (12 Replies)
Discussion started by: Mohin Jain
12 Replies

5. Shell Programming and Scripting

Remove line breaks after a match

I need to remove all line breaks in a document after a match, until there is a blank line. Example below, after the match "THE GREEN TABLE" remove line breaks until a blank line. Then, after the match "THE BLUE TABLE" do the same. Before: THE GREEN TABLE Lorem ipsum dolor sit amet,... (14 Replies)
Discussion started by: dockline
14 Replies

6. Shell Programming and Scripting

shell script to remove extra commas from CSV outp file

Name,,,,,,,,,,,,,,,,,,,,Domain,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Contact,Phone,Email,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Location -----------------------,------------------------------------------------,-------,-----,---------------------------------,------------------------------------ ----... (1 Reply)
Discussion started by: sreenath1037
1 Replies

7. Shell Programming and Scripting

How to remove particular line from file through shell script?

Hi, I am pasring a file line by line. I need to check each field in line and remove particular line. input file lines are, 02;ABC;PQR 03;aaa;rrr 04;ABC;ggg 09;eee;ABC 04;lmn;stu I am looking for line containing "ABC" as field value. Now How can I remove this line from input file... (7 Replies)
Discussion started by: Poonamol
7 Replies

8. Shell Programming and Scripting

Piped input to sed 's/\n/ /' doesn't remove the line breaks..

Using ls input as example.. ls | sed 's/\n/ /'outputs with line breaks, where I was expecting the \n to disappear. I've tried \r as well wondering if terminal output used different breaks. Is there a way to remove the line breaks without saving to file and then working from there? ----------... (2 Replies)
Discussion started by: davidpbrown
2 Replies

9. Shell Programming and Scripting

How to append value at first line of CSV file using shell script?

I have an issue where I need to append a value at the last of the csv, I have created a shell script and it is appending the columns at the last but it is appending at all lines, and my requirement is specific to just append at the 1st line. Have a look and suggest, (7 Replies)
Discussion started by: anujrichhariya
7 Replies

10. Shell Programming and Scripting

any better way to remove line breaks

Hi, I got some log files which print the whole xml message in separate lines: e.g. 2008-10-01 14:21:44,561 INFO do something 2008-10-01 14:21:44,561 INFO print xml : <?xml version="1.0" encoding="UTF-8"?> <a> <b>my data</b> </a> 2008-10-01 14:21:44,563 INFO do something again I want... (3 Replies)
Discussion started by: csmklee
3 Replies
Login or Register to Ask a Question