remove lines from file

Hi gurus,

i'm trying to remove a number of lines from a large file using the following command:

sed '1,5000d' oldfile > newfile

Somehow the lines in the old file are not deleted...

Am I doing this wrongly? Any suggestions? :confused:

Thanks! :slight_smile:

wee

What do you expect ?
If you want to delete in original file read man sed for -i option.

like this:

sed -i '1,5000 d' oldfile

from the GNU sed manual:

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

hey gurus...

perhaps i did not make myself clear...

well i have this really huge file which i cannot open up using vi (due to the constraint in which /tmp buffer could not hold)

so i need to extract out the oldest information within the file (say maybe the first 10000 lines) and pipe out to another file while at the same time the 10000 lines will be removed from the old file.

which is why i'm using sed to do it but somehow it's not matching my ideal results...

please advise. thanks.

wee

first check wthether you have latest version of sed which comes with -i option
you can check this in man page if you have that you can do

sed -i '1,10000d' huge_file > temp_file

if you don't have the -i option

sed -n '1,10000p' huge_file > file1
sed -n '10001,$p' huge_file > temp_file
cat temp_file >huge_file

sorry i dont have the -i option for sed...probably this is the older version.

will try out the other option.

update later. thanks. :slight_smile:

wee

hi what does $p stands for? variable?

somehow it does not seem to do what i'm expected it to do...

supposed i have this file with 200000 lines in it, so what i'm trying to do is to remove the first 100000 lines and pipe it to another file so that i can use vi to open the original file.

hmm...sounds complicated? :rolleyes:

thanks gurus. :slight_smile:

wee

$p is not a variable $ stands for last line that is end of file
since you don't have -i option in sed first copy 100000 line to file1
then copy from line 100001 to last line to temp file then move temp file to huge file finally file1 will have 100000 line and huge file will have remaining lines

sed -n '1,100000p' huge_file > file1
sed -n '100001,$p' huge_file > temp_file
mv temp_file huge_file

No, you didn't :wink:

Maybe you want to take a look at : man split

hi vidyadhar85,

thanks for the explaination :slight_smile:

so what you recommend is to move the first 100000 lines to file1 and the remaining lines to temp file and use the temp file to overwrite the existing huge file. is that right?

wee

sed -n '10001,$p' huge_file > temp_file

               10001   ,            $                  p       huge_file > temp_file

(from the line 10001)(to)(till the endof the file)(print)

hope this helps