LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   deleting 100 rows in text file (1-100) via terminal (http://www.linuxquestions.org/questions/linux-newbie-8/deleting-100-rows-in-text-file-1-100-via-terminal-404436/)

itz2000 01-17-2006 08:28 AM

deleting 100 rows in text file (1-100) via terminal
 
deleting 100 rows in text file (1-100) via terminal

how can I do that?

homey 01-17-2006 09:03 AM

sed 1,100d file.txt > new_file.txt

itz2000 01-17-2006 01:29 PM

thanks.... can I though write it to same file?

sed 1,10 file.txt > file.txt
right?

frob23 01-17-2006 03:33 PM

No. Do not do that because you will end up with an empty file.

You need to use the temporary file for this because the redirection empties the file before anything happens.

You can use the following to do it in one step. Note: the && means to only execute the mv command if the first command returns without an error.
Code:

sed '1,100d' file.txt > tmpfile.txt && mv tmpfile.txt file.txt

cs-cam 01-17-2006 05:42 PM

Code:

sed -e '1,100d' -i file.txt
Will make the changes inside the file and avoid the need for the hackish file work done above.

wmakowski 01-17-2006 11:19 PM

If you want to use an editor, vi can do that pretty easily.

1) vi file.txt
2) while on first line of file.txt type 100dd
3) :wq

Bill


All times are GMT -5. The time now is 09:34 PM.