Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-20-2013, 08:13 PM   #1
Registered: Jan 2006
Location: Dallas
Distribution: Slackware
Posts: 880

Rep: Reputation: 172Reputation: 172
sed or awk delete LF terminated lines from file

I have some files that should not have any linefeed (newline) characters in them, but they've been corrupted with the addition of a few lines of text at the very beginning of the file that are terminated with a line ending character. For example:

This is a line of text\n
This is another line\n
And another\n
where \n above is x'0a', the newline or linefeed character. The rest of the file following the third line in the example above is also text data, but it doesn't (and shouldn't) have any line ending characters.

For this example, wc -l will tell me I have four lines, even though the last one does not have a line ending character.

I can delete the garbage lines at the beginning like this:

$ wc -l file
4 file
$ sed '1,3d' file
How could I combine these into one command so that the output from the wc command can be used in the sed command to delete all the lines before the last one? It seems like this should be pretty easy, but I can't figure it out.

Another way to think of this is that I want to retain only the last "line" of the file. I want to delete all lines from the file except for the last line. Does that suggest another solution?


Last edited by Z038; 12-20-2013 at 08:21 PM.
Old 12-20-2013, 08:34 PM   #2
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,809

Rep: Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637Reputation: 637
Originally Posted by Z038 View Post
I want to delete all lines from the file except for the last line.
Three ways:
tail -1 $InFile >$OutFile

sed '$!d' $InFile >$OutFile

awk 'END{print}' $InFile >$OutFile
Daniel B. Martin

Last edited by danielbmartin; 12-20-2013 at 08:39 PM.
2 members found this post helpful.
Old 12-20-2013, 09:18 PM   #3
Registered: Jan 2006
Location: Dallas
Distribution: Slackware
Posts: 880

Original Poster
Rep: Reputation: 172Reputation: 172
Thank you Daniel! The solution turned out to be far simpler than I imagined.

I think I like the tail command the best.
Old 12-21-2013, 01:07 AM   #4
Senior Member
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 274Reputation: 274Reputation: 274

sed -n '$p' $InFile >$OutFile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Recognize multi-line pattern and delete certain lines (awk/sed?) legato Programming 4 10-31-2012 05:02 PM
[SOLVED] Delete Lines : after pattern1 and between pattern2 and patter3 using awk/sed/perl vk2012 Linux - Newbie 6 06-10-2012 12:02 PM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 10:50 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 09:59 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 10:19 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration