Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete line from file at specified line number in bourne shell [duplicate]

Tags:

shell

unix

sed

I'm making an appointment tracking script in Bourne Shell and need to delete an appointment from the text file. How do I delete a line from a file leaving no white space if I have the line number? The file looks like this:

1:19:2013:Saturday:16.00:20.30:Poker  
1:24:2013:Thursday:11.00:11.45:Project meeting  
1:24:2013:Thursday:14.00:15.10:CSS Meeting
like image 276
user1731199 Avatar asked Mar 14 '13 02:03

user1731199


1 Answers

To delete line 5, do:

sed -i '5d' file.txt

For a variable line number:

sed -i "${line}d" file.txt

If the -i option isn't available in your flavor of sed, you can emulate it with a temp file:

sed "${line}d" file.txt > file.tmp && mv file.tmp file.txt
like image 167
John Kugelman Avatar answered Sep 25 '22 01:09

John Kugelman