Visit Our UNIX and Linux User Community


Remove line feed from csv file column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove line feed from csv file column
# 1  
Old 06-07-2011
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 ), since the end of the line is also line feed value is C7 is pushed to different line .
Code:
1) 1,b2,c3,d4,e5,f6,here comes the line feed ,g8,h9,i10
                    this is pushed to second line 
 
the output is :
 
1,b2,c3,d4,e5,f6,here comes the line feed
this is pushed to second line , g8,h9,i10

kindly advice
# 2  
Old 06-07-2011
Code:
 awk -F"," '{if(NF>7){for(i=8;i<=NF;i++){a=a","$i;$i=""};NF=7;print $0;getline;gsub(/^ */,"");OFS="";print $0,a}else{print $0}}' OFS="," file.csv

# 3  
Old 06-07-2011
Code:
use Text::CSV;  
   
my $csv = Text::CSV->new({binary=>1});  
   
#NOTE: Replace input.txt with your actual filename  
open(my $in, "<input.txt") or die "input: $!\n";  
   
#NOTE: Replace output.txt with the name you want for your new file  
open(my $out, ">output.txt") or die "output: $!\n";  
   
while(my $colref = $csv->getline ($in)) {  
        s/[\r\n]//g foreach (@$colref);  
        $csv->combine(@$colref);  
        print $out $csv->string . "\n";  
}  
close($in);  
close($out);

Reference : How to remove all CR/LF from a line except last one? : Database Export, Perl, Remove CR and LF

Previous Thread | Next Thread
Test Your Knowledge in Computers #296
Difficulty: Easy
Linux Mint 2.0 was based on Ubuntu 6.10 and used the Ubuntu 6.10 package repositories and using it as a codebase.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove line feed in data

Please use code tags for sample data Hi I have a file where there are line feeds in the data. I am not able to read the file from an application. I exported this data from Access database and many columns contain line feed. My data looks like this abcd,efgh,ijkl,mnop abcd,ef... (7 Replies)
Discussion started by: dnat
7 Replies

2. Shell Programming and Scripting

Need a piece of shell scripting to remove column from a csv file

Hi, I need to remove first column from a csv file and i can do this by using below command. cut -f1 -d, --complement Mytest.csv I need to implement this in shell scripting, Whenever i am using the above command alone in command line it is working fine. I have 5 files in my directory and... (3 Replies)
Discussion started by: Samah
3 Replies

3. Shell Programming and Scripting

Want to remove a line feed depending on number of tabs in a line

Hi! I have been struggling with a large file that has stray end of line characters. I am working on a Mac (Lion). I mention this only because I have been mucking around with fixing my problem using sed, and I have learned far more than I wanted to know about Unix and Mac eol characters. I... (1 Reply)
Discussion started by: user999991
1 Replies

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

5. Shell Programming and Scripting

awk remove line feed

Hi, I've this file: 1, 2, 3, 4, 5, 6, I need to remove the line feed LF every 3 row. 1,2,3, 4,5,6, Thanks in advance, Alfredo (5 Replies)
Discussion started by: alfreale
5 Replies

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

7. Shell Programming and Scripting

PHP header text/csv unwanted line feed character

We have successfully created a comma delimited file, the results are correct from an sql database query. When using the following headers to print the file using print $data, an extra line feed is added and a blank row appears on the top of the data in Excel: header("Expires: 0"); ... (0 Replies)
Discussion started by: ifimbres
0 Replies

8. Shell Programming and Scripting

SED remove line feed and add to certain area

Hi All, I have a xml file and requirement is to remove the line feed and add line feed after some element. <?xml version="1.0" ?> <AUDITRECORDS> <CARF> <HED> <VN1>20090616010622</VN1> <VN2>0</VN2> <VN3>1090</VN3> <VN4>CONFIG_DATA</VN4> ... (8 Replies)
Discussion started by: sreejitnair123
8 Replies

9. Shell Programming and Scripting

need script-remove line feed

hi all, i have csv file with three comma separated columns i/p file First_Name, Address, Last_Name XXX, "456 New albany \n newyork, Unitedstates \n 45322-33", YYY\n ZZZ, "654 rifle park \n toronto, canada \n 43L-w3b", RRR\n is there any way i can remove \n (newline) from the second... (1 Reply)
Discussion started by: gowrish
1 Replies

Featured Tech Videos