LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-20-2013, 07:13 PM   #1
Z038
Member
 
Registered: Jan 2006
Location: Dallas
Distribution: Slackware
Posts: 910

Rep: Reputation: 174Reputation: 174
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:

Code:
This is a line of text\n
This is another line\n
And another\n
original-file-data-no-terminator...
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:

Code:
$ 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?

Thanks!

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

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

sed '$!d' $InFile >$OutFile

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

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

Original Poster
Rep: Reputation: 174Reputation: 174
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, 12:07 AM   #4
kooru
Senior Member
 
Registered: Sep 2012
Posts: 1,385

Rep: Reputation: 275Reputation: 275Reputation: 275
Or:

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


Reply



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 04:02 PM
[SOLVED] Delete Lines : after pattern1 and between pattern2 and patter3 using awk/sed/perl vk2012 Linux - Newbie 6 06-10-2012 11:02 AM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM
Delete specific Range of lines Using sed , awk, grep etc. joyds219 Linux - Newbie 4 03-28-2008 08:59 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

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