Visit Our UNIX and Linux User Community


delete last line from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete last line from text file
# 1  
Old 05-30-2002
delete last line from text file

I have a file with which i must remove the last line of the file (the footer). I know how to copy and redirect with tail -1 etc, but how do i delete it permanentely
# 2  
Old 05-30-2002
My way to delete the last line of a file is with sed...
Code:
sed '$d' < input

Of course, to make this permanent, you need something like:
Code:
sed '$d' < input > tempfile
mv input input.old
mv tempfile input


Last edited by Yogesh Sawant; 02-25-2011 at 08:27 AM.. Reason: added code tags
# 3  
Old 06-04-2002
Same as Perderabo's but a one liner

sed '$d' < file1 > file2 ; mv file2 file1
This User Gave Thanks to LANkrypt0 For This Post:
# 4  
Old 06-25-2002
Hi,

What does '$d' signify here? What if i want to delete the second last line. Do i have to use '$2d'?

Can you clarify?

Thanks,
Nisha
# 5  
Old 06-25-2002
$ means the last line. But sed doesn't know that it has the last line until it arrives. You can't do a "sed '$-1d'" or anything. Deleting the next to the last line via sed is very hard. So hard that I would normally use other techniques. But just for the heck of it, I finally got a sed script running that deletes the next to the last line:
Code:
#! /usr/bin/sed -nf
#
${P
q
}
1{h
d
}
x
p

And this solution will not easily scale up. A sed script to delete $-2 will take about 10 times the code. Deleting $-3 would be a nightmare. The problem is that you don't know in advance how many lines there are. To really have a general solution, you need to make two passes through the file. On the first pass, you count the lines. On the second pass, you delete the appropriate line. sed cannot do this alone since it always will only make a single pass through the file.
Code:
#! /usr/bin/ksh
lines=$(wc -l $1)
((target=lines-1))
sed "${target}d" < $1
exit 0

Now we have an extensible solution.

Previous Thread | Next Thread
Test Your Knowledge in Computers #436
Difficulty: Easy
React is an open source JavaScript library.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete lines above and below specific line of text

I'm trying to remove a specific number of lines, above and below a specific line of text, highlighted in red: <STMTTRN> <TRNTYPE>CREDIT <DTPOSTED>20151205000001 <TRNAMT>10 <FITID>667800001 <CHECKNUM>667800001 <MEMO>BALANCE </STMTTRN> <STMTTRN> <TRNTYPE>DEBIT <DTPOSTED>20151207000001... (8 Replies)
Discussion started by: bomsom
8 Replies

2. UNIX for Dummies Questions & Answers

Delete records based on a text file from a text file

Hi Folks, I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted. Data.txt state city zone Alabama Huntsville 4 California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies

3. UNIX for Dummies Questions & Answers

How to delete text between two characters in line?

Hi, I have a large text file with the following format: >gi|347545744|gb|JN204951.1| Dismorphia spio voucher 5 ATCAAATTCCTTCCTCTCCTTAAA >gi|17544664774|gb|WN204922.32| Rodapara nigens gene region CCGGGCAAATTCCTTCCTCTCCTTAAA >gi|555466400|gb|SG255122.8| Bombyx mandariana genbank 3... (1 Reply)
Discussion started by: euspilapteryx
1 Replies

4. Shell Programming and Scripting

Find the text in the file and delete the rest of the line.

Hi, I have one requiremnet like this. I need to find some particular string (eg.IncludeDateTime = ) in a file. And wherever it finds the string the unix script has to delete the remaining text coming after the string (ie., 'IncludeDateTime = ' ) in the same line. I tried to write this script in... (5 Replies)
Discussion started by: jannusuresh
5 Replies

5. Shell Programming and Scripting

Delete the last empty/blank line of the text file

Hi All, I have a file.txt which seems like having three lines. wc -l file.txt 3 file.txt In fact, once it is open in text editor, this file has four lines where the last line is empty. how can i delete this last empty line of the file.txt? I tried the codes below so far but they... (6 Replies)
Discussion started by: senayasma
6 Replies

6. UNIX for Dummies Questions & Answers

How to delete one line from a text file

PATTERN="" sed "/$PATTERN/d" $file In the file data is stored as ::: ::: I need to delete only the line containing the PATTERN pls help to point out why the above code is not working (4 Replies)
Discussion started by: thomsandy
4 Replies

7. UNIX for Dummies Questions & Answers

Delete line in text file

Hi, How do I go about deleting a line in a text file. I have used grep to find a particlular word, now how do I delete the line? so far I got: grep -i $keyword file.txt (6 Replies)
Discussion started by: Berserk
6 Replies

8. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

9. UNIX for Dummies Questions & Answers

how to delete line with matching text and line immediately after

hello experts, I have a file: File1 Sample Test1 This is a Test Sample Test2 Another Test Final Test3 A final Test I can use sed to delete the line with specific text ie: sed '/Test2/d' File1.txt > File2.txt How can I delete the line with the matching text and the line immediately... (6 Replies)
Discussion started by: orahi001
6 Replies

10. Shell Programming and Scripting

Delete first line from any text file ?

What command I can use to delete first line from any text file ? Thk (5 Replies)
Discussion started by: aungomarin
5 Replies

Featured Tech Videos