Remove lines

i want to remove the records starting with abc or xyz in the file without redirecting the original file.

sample : file1.txt

abc1234
124234
2020202
3242342
xyz2342
2afafa

Expecting output
124234
2020202
3242342
2afafa

What does "redirecting the original file" mean?

egrep -v "^(abc|xyz)" < infile > outfile ; mv outfile infile

You won't find a solution for deleting lines which doesn't replace the original file. Even sed -i has to replace the original file, it just does it for you.

 infile should not be redirected to outfile 

The mv would loose the inode of infile
instead:

cat outfile>infile
rm outfile

or you could just work with your outfile without taking car of infile if you don't want to modify it :slight_smile:

Thanks.

Using sed and the -i option (not all sed support it, see 'man sed' for more infos)

sed -ri '/^(abc|xyz)/d' file1.txt

Still replaces the original file, just does it for you, like I said above. This can have the side-effect of changing ownership of files when you run sed as a different user for example...

if your sed don't support -i option, here is the code without redirecting the original file.

{ rm input; sed -r '/^(abc|xyz)/d'  > input; } < input

or you just need keep the record start from number

{ rm input; grep ^[0-9]  > input; } < input