Remove new line at the end of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove new line at the end of each line
# 1  
Old 05-13-2010
Remove new line at the end of each line

Code:
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer","10","21
"
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer","10","21
"

I have newline chars at the end of the row, how do i remove them using awk ?

Moderator's Comments:
Mod Comment Please use code tags


---------- Post updated at 05:40 PM ---------- Previous update was at 04:43 PM ----------

Code:
awk -F "," '$NF { a=$0; getline; $0=a$0 } 1 ' input.txt > output.txt

I tried the above but it worked only for few lines not to the end of the file. My file is huge.

Last edited by Scott; 05-13-2010 at 06:37 PM..
# 2  
Old 05-13-2010
Hi.

I did something similar:
Code:
$ awk '{print $0 "\""; getline}' file1
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer", "10","21"
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer", "10","21"

In sed:
Code:
$ sed "N; s/\n//" file1
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer", "10","21"
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer", "10","21"

# 3  
Old 05-13-2010
Code:
 awk '{printf ($0~/"$/)?$0"\n":$0}' input.txt

# 4  
Old 05-14-2010
Both the solutions works fine. If there is no data in the last column the script fails.

---------- Post updated at 07:32 AM ---------- Previous update was at 12:54 AM ----------

Code:
awk '{printf ($0~/"/)?$0"\n":$0}'

would simple ignore everything.

Any idea how to remove newline when there is no value
Code:
"376964582","1","rotated","2010-05-06 12:40:47","1205847472","436201","444070","444070","sv","663","dm","Exceeded","2","2001","21","deer","10","
"


Last edited by vgersh99; 05-14-2010 at 08:35 AM.. Reason: fix tag
# 5  
Old 05-14-2010
Code:
cat file | tr -d '\015'

----

This wasn't the answer. Need to read ... but this is answer if somebody ask to how remove carriage return ..

Change ORS, default is newline. Here is ,. Do you need separator between lines or not ? If not, set ORS="".
Code:
awk 'BEGIN {  ORS="," }
{print $0}' file


Last edited by kshji; 05-14-2010 at 12:49 PM..
# 6  
Old 05-14-2010
Code:
cat filename|tr -d '\n' >filename.raw

If the input file is fixed-length records we could use "dd".

The conversion is a very strange operation to do because the converted file would be unreadable by any normal record based software.

It would help to know what the problem is with the original file?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

2. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

3. UNIX Desktop Questions & Answers

To remove the extra spaces at the end of each line in a file

I have a file of about 10k records and eace line is having an extra space of 5 byte at the end.. Iwant to remove the extra spaces at the end of each line.. Can someone please help me out.. I tried using sed command and its not working... can someone please help me out. (3 Replies)
Discussion started by: rammohan
3 Replies

4. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

5. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

6. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

7. Shell Programming and Scripting

How to remove new line character at end of file.

I need to remove new line character from end of file. Suppose here are content. a|b|c|d|r a|b|c|d|r a|b|c|d|r <new line> that means file contains 4 lines but data is there in 3 lines. so I want that only 3 lines should be there in file. Please help (20 Replies)
Discussion started by: varun940
20 Replies

8. Shell Programming and Scripting

Remove the spaces at the end of a line starting from a fixed position

I want to remove the trailing spaces at the end of each line starting from a particular position(using ksh script). For example, in the attached file, I want to remove all the spaces starting from the position 430 till the end. The space has to be removed only from the 430th position no matter in... (3 Replies)
Discussion started by: Suryaaravindh
3 Replies

9. Shell Programming and Scripting

Remove line starting from space till end.

Hi, I have a code tag, from which i have the below snippet: intelrpt.GetCMB_FB type=ODBC> intelrpt.GetCMB_FB type=SYBASE> I want the output like: intelrpt.GetCMB_FB intelrpt.GetCMB_FB That is remove the lines starting from WHITESPACE till end. Please help. I am new to... (7 Replies)
Discussion started by: anupdas
7 Replies
Login or Register to Ask a Question