Column with New Line in CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column with New Line in CSV file
# 1  
Old 09-08-2014
Column with New Line in CSV file

Hello,

Got a CSV file which contains 21 columns

Need to convert the file to Pipe delimiter and Few columns text data contains new line

Example

Code:
1,2,3,"ABC" 8" ABC,5,6,7 
1,2,3,"ABC"
8" ABC,5,6,7  ( New Line)
1,2,3,""ABC" 8" ABC", 5,6,7
1,2,3,"ABC"
,5,6,7(New line)

Expected output
Code:
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|"ABC"|5|6|7

# 2  
Old 09-08-2014
For your sample input, try something like this:-
Code:
awk -F, 'NF!=7{ORS=(ORS!=" "?" ":RS)}{$1=$1}1' OFS=\| file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 09-08-2014
Yoda Thanks a lot, Can you please explain me whats going on with that statement.

I am new to AWK commands.
# 4  
Old 09-08-2014
Code:
awk -F, '
        # if NF not equal to 7
        NF != 7 {
                # If ORS not equal to " " set ORS = " " else set ORS = RS (newline by default)
                ORS = ( ORS != " " ? " " : RS )
        }
        {
                # This stmt rebuilds current record, this is to set OFS = |
                $1 = $1
        }
        # 1 == true. If true, default awk operation is to print current record
        1
' OFS=\| file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 09-08-2014
Yoda, I always love your awk replies. They're very economical and clever with the concepts of awk. Thanks.
This User Gave Thanks to treesloth For This Post:
# 6  
Old 09-08-2014
Nice Yoda.. Just for fun ORS=(ORS!=" "?" ":RS) could be reduced to ORS=ORS?x:RS still
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-09-2014
Hello,

One more approach for same.

Code:
awk -F, '/^1/ {a=1; OFS="|";v=$0;b=1} {if(a==1 && $0 !~ /^1/){v=v" "$0;b=0}} !b{gsub(/\,/,"|",v);print v} (a && b && NF==7){gsub(/\,/,"|",v);print v}' filename

Output will be as follows.

Code:
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|"ABC" 8" ABC|5|6|7  ( New Line)
1|2|3|""ABC" 8" ABC"| 5|6|7
1|2|3|"ABC" |5|6|7(New line)

EDIT: Adding one more solution for same.

Code:
awk -F"," '{if($NF ~ "ABC"){ORS=" "} else {ORS="\n"}}{$1=$1} 1' OFS="|"  test14

Output will be as follows.

Code:
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|"ABC" 8" ABC|5|6|7
1|2|3|""ABC" 8" ABC"| 5|6|7
1|2|3|"ABC" |5|6|7

Where test14 is input file name.

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-09-2014 at 03:29 AM.. Reason: Added one more solution
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get maximum per column from CSV file, based on date column

Hello everyone, I am using ksh on Solaris 10 and I'm gathering data in a CSV file that looks like this: 20170628-23:25:01,1,0,0,1,1,1,1,55,55,1 20170628-23:30:01,1,0,0,1,1,1,1,56,56,1 20170628-23:35:00,1,0,0,1,1,2,1,57,57,2 20170628-23:40:00,1,0,0,1,1,1,1,58,58,2... (6 Replies)
Discussion started by: ejianu
6 Replies

2. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

3. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

5. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

6. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

7. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

8. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies

9. UNIX for Dummies Questions & Answers

read a line from a csv file and convert a column to all caps

Hello experts, I am trying to read a line from a csv file that contains '.doc' and print the second column in all caps. e.g. My csv file contains: Test.doc|This is a Test|test1|tes,t2|test-3 Test2.pdf|This is a Second Test| test1|tes,t2|t-est3 while read line do echo "$line" |... (3 Replies)
Discussion started by: orahi001
3 Replies
Login or Register to Ask a Question