Why sed command deletes last line in a file if no carriage return?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why sed command deletes last line in a file if no carriage return?
# 1  
Old 06-24-2011
Why sed command deletes last line in a file if no carriage return?

Hi

I am using sed command to make SCORE=somevalue to SCORE=blank in a file.

Please see the attached lastline.txt file. After executing the below command on the file, it removes the last line.
Code:
cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt

Why does sed command remove the last line in the file if there is no carriage return?

Also if i use echo \\r >> lastline.txt and then execute the command it works fine and the last line is not removed.

Also, How to check if the last line already has a carriage return or not ? and add if there is no carriage return?

Input File : lastline.txt
Code:
[KEY A]
TABLE=TableA
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreA
[KEY B]
TABLE=TableB
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreB
[KEY C]
TABLE=TableC
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreC
[KEY D]
TABLE=TableD
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreD
[KEY E]
TABLE=TableB
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreB
[KEY F]
TABLE=TableF
KEY=KEYF
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreF
[KEY G]
TABLE=TableA
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreG
[KEY H]
TABLE=TableB
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreH
[KEY I]
TABLE=TableA
KEY=KEYA
DESC=VALUE
ORDERBY=Id
SCORE=C:\Common\TestscoreI

Please note that there should not be any blank line at the bottom of input file. No carriage return on the last line.

Thanks
Ashok

Last edited by Franklin52; 06-24-2011 at 03:52 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 06-24-2011
Have a read of this post:

https://www.unix.com/shell-programmin...ne-file.html#5

This is Useless Use of Cat:
Code:
cat lastline.txt | sed 's/SCORE=.*$/SCORE=/g' > newfile.txt

This should be sufficient:
Code:
sed 's/SCORE=.*$/SCORE=/g' lastline.txt > newfile.txt

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 06-24-2011
Check last line in a file ended with newline or not

Hi,

How to check if the last line in a file has ended with carriage return or not?

How to check this in shell script and and add new line programatically?

Thanks
Ashok
# 4  
Old 06-24-2011
One way could be something like this:
Code:
file="lastline.txt"

lastchar=$(od -An -t dC $file |awk 'NF{c=$NF}END{print c}')
if [ $lastchar = "10" ]
then
  echo "file is OK"
else
  echo >> $file
fi

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Command to add Carriage Return and Line Feed

Hello, Can someone please share a Simple AWK command to append Carriage Return & Line Feed to the end of the file, If the Carriage Return & Line Feed does not exist ! Thanks (16 Replies)
Discussion started by: rosebud123
16 Replies

2. Shell Programming and Scripting

Need Help to delete carriage return and new line in csv file

Hi All, I have a problem loading the data from a csv file As you see below in the Input ,For the Data starting with " there are 2 lines, which i want to make them into single without changing the format of that data. You can see the desired output below: While i try to open the csv file and... (4 Replies)
Discussion started by: mlavanya
4 Replies

3. Shell Programming and Scripting

Create Carriage Return using SED

I am hoping someone can help me with a solution to this problem using SED. My issue is actually two-fold. First, I have an order file that is sent to us from another system, however, there has been a change in current processes that requires a carriage return added to the end of each order. The... (5 Replies)
Discussion started by: chino_1
5 Replies

4. UNIX for Dummies Questions & Answers

use sed to replace whitespace with a carriage return

Greetings I need to replace "whitespace" in a file with the newline character aka carriage return My command is either wrong or not interpreted properly by me shell sed s/" "/\\n" "/g nets > nets1 or sed s/" "/\n" "/g nets > nets1 nets (input file) 13MHZ_IN... (4 Replies)
Discussion started by: awk_sed_hello
4 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. Shell Programming and Scripting

Delete carriage return in SED

Hi everybody! I'm working in one script with sed, I have file with the next content: <voms.db.type value="changeme"/> <voms.db.host value="changeme"/> <voms.admin.smtp.host value="changeme"/> <voms.mysql.admin.password value="changeme"/> <glite.installer.verbose value="true"/> ... (3 Replies)
Discussion started by: juedsivi
3 Replies

7. Shell Programming and Scripting

How to delete carriage return in SED

Could someone tell me how to do the below without opening the file? (eg in sed or awk) I have a file with the contenst below: $ more file1.txt 10 AAA; 200 BBB; 3 CCC; I want to delete the carriage return of one line above the line which has ";" at the end to get the... (3 Replies)
Discussion started by: stevefox
3 Replies

8. Shell Programming and Scripting

Removing Carriage Return and or line feed from a file

Hello I'm trying to write a shell script which can remove a carriage return and/or line feed from a file, so the resulting file all ends up on one line. So, I begin with a file like this text in file!<CR> line two!<CR> line three!<CR> END!<CR> And I want to end up with a file... (1 Reply)
Discussion started by: tbone231
1 Replies

9. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies

10. Shell Programming and Scripting

carriage return/line feeds

Hello, I have a file that has got carriage returns in it and I want to take them out. Anyone know how I can do this in a ksh? thanks (4 Replies)
Discussion started by: pitstop
4 Replies
Login or Register to Ask a Question