How to remove new line character at end of file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove new line character at end of file.
# 15  
Old 04-12-2012
A noddy way using a shell script would be:-
Code:
#!/bin/ksh
while read line
do
   echo "$line\c"
done < inputfile > outputfile

Be warned that if you have a large file, this could be horribly slow.




Robin
Liverpool/Blackburn
UK
# 16  
Old 04-12-2012
What exactly do you want to do? Remove blank lines from your input, remove all newline characters, or just remove the last newline character?

Also, you never told us what distribution (OS) you're using.
# 17  
Old 04-12-2012
Code:
#!/bin/ksh
while read line
do
   echo "$line\c"
done < inputfile > outputfile

This removed all new line characters. I want only last to be removed.

Ques: What exactly do you want to do? Remove blank lines from your input, remove all newline characters, or just remove the last newline character?
Ans: I want only last new line character to be removed.

i.e.
My data is
Code:
A<new line>
B<new line>
c<new line>
D<new line>

So If I open the file cursor is at the new line below D. I want that cursor should be just after D. I want only last <new line> to be removed.

Ques: OS distribution.
Ans: Sorry, please tell me how to check.

---------- Post updated at 09:37 PM ---------- Previous update was at 08:54 PM ----------

Code:
last=$(tail -1 echo_hsa_file.txt)
sed '$d' < echo_hsa_file.txt > echo_hsa_file1.txt
echo "${last}"\\c  >> echo_hsa_file1.txt


This worked.Thanks all for all the help.

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-12-2012 at 01:17 PM..
# 18  
Old 04-12-2012
I am guessing the OP was working with a DOS file, and that that is why all the options did not work, since the lines are not empty because of the Carriage Return present at the end of each line, before the linefeed. The last option did work since it deleting the last line, no matter what is on there...

I suggest trying this first:
Code:
tr -d '\r' < infile > outfile

Before trying any of the other suggestions.
# 19  
Old 04-12-2012
We think alike. My guess was that there was a MSDOS ctrl/Z character at the end of the file.
# 20  
Old 04-12-2012
Another way of removing last newline character

Code:
$$ C=`wc -c < filename`
$$ D=`tail -c +$C < filename`
$$ if [ $D = $CR ]
then
C=`expr $C - 1`
head -c +$C < filename >filename
fi

This code can be used to traverse a list of filenames in a shell script and removing the last newline charaacter from it if it is present in that file.
# 21  
Old 04-13-2012
Code:
 
sed '$d' file
 
Test environment : centos 5.5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character tried the below options, 1. perl -p -i -e 's/^Z//g' new.txt 2. perl -0777lwi -032e0 new.txt and Sed command, dos2unix etc it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE ... (7 Replies)
Discussion started by: prsam
7 Replies

4. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

5. Shell Programming and Scripting

add character to every end-of line in file

Hi All I have a file which conatins record.the length of every records is 47. problem : in the end of record i don't have a "\015" character. i want to add this "\015" charcter in the end of every record. the file contains something like 700 records. i've tried with sed command - nothing. ... (8 Replies)
Discussion started by: naamas03
8 Replies

6. Shell Programming and Scripting

add character to the end of each line in file

hi all i have 32 lines in file. the length of each line is 82 , i want that in the end of each line , means in postion 83-84 to put two characters 0d(=\015), 0a(=\012) i want that the 0d will be in postion 83 and the 0a will be in postion 84 in each line of the file how shall i do it ? ... (7 Replies)
Discussion started by: naamas03
7 Replies

7. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

8. UNIX for Dummies Questions & Answers

How to add new line character at the end of a file

hi all, i have this question: How to add new line character at the end of a file???? i need this because i am loading a file to sybase and i have problems with the last record thanks for your help (5 Replies)
Discussion started by: DebianJ
5 Replies

9. UNIX for Dummies Questions & Answers

removing a character and addending to end in each line in a file

HI i am having a file this (sys19:pnlfct:/pfact/temp>) cat temp_sand 1234567890 1234567890 1234567890 1234567890 I want to make this file as (sys19:pnlfct:/pfact/temp>) cat temp_sand 1456789023 1456789023 1456789023 1456789023 just take the 2nd and 3rd position and put it... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

10. UNIX for Advanced & Expert Users

Deleting end of line $ character in file

Hi, I've got a file where in the middle of the record is a $ end of line character, visible only when I open the file in vi and do :set list. How to I get rid of the character in the middle and keep it at the end. The middle $ character always appears after SW, so that can be used to tag it.... (3 Replies)
Discussion started by: bwrynz1
3 Replies
Login or Register to Ask a Question