Operating System - HP-UX
1753535 Members
7966 Online
108795 Solutions
New Discussion юеВ

Re: how to remove content in a file

 
SOLVED
Go to solution
ust3
Regular Advisor

how to remove content in a file

I hv a text file that has 500000 lines , some lines begins the word "Error" , if I want to delete all lines except the lines begins the word "Error" ( that means only keep "Error" lines ) , what can I do ? thx
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: how to remove content in a file

grep "Error" filename > newfile


Pete

Pete
Marcin O.
Frequent Advisor

Re: how to remove content in a file

Hi

ex.
cat file1 | grep "^Error" >file2

Marcin
James R. Ferguson
Acclaimed Contributor

Re: how to remove content in a file

Hi:

Using 'grep' is certainly one way to do this. As shown by Marcin, you probably want to anchor your match to the beginning of the line since you said '...except lines [that begin with] the word "Error"'.

It is wasteful to 'cat' the file into a pipe to 'grep'. That adds another process!

You could also use Perl and to select lines beginning with "Error" and having a word-boundry on either side (\b).

# perl -ne 'print if /\bError\b/' file

Regards!

...JRF...
Marcin O.
Frequent Advisor

Re: how to remove content in a file

Hi
Of course all above methods are correct, but I check speed of processing those three methods. If you interested in

1. only grep
2. cat+grep
3. perl
on file with 4M lines, result was:
1. 4.1s
2. 4.5s
3. 9.5s

Marcin
Hein van den Heuvel
Honored Contributor

Re: how to remove content in a file

ust3,

Please be VERY explicit about 'Error'.
Spelled EXACTLY so, or more upper/lower combinations
Exactly in the beginning of the line, or 'close' (how close!).
Or are you looking for 'Error' anywhere as seperate word(the first perl solution)?

Marcin,

Whiel you are trying... please re-try perl again with an anchorred RE where it does not have to scan the whole line:

# perl -ne 'print if /^Error/' file


Hein.
James R. Ferguson
Acclaimed Contributor

Re: how to remove content in a file

Hi (again):

Ooops, thanks Hein, I beat the anchor drum and then mistyped myself :-{

Also, while I would expect Perl to have a small startup overhead, its value in cases like this lies in its superior regular expression engine.

Regards!

...JRF...
Sandman!
Honored Contributor
Solution

Re: how to remove content in a file

# awk '$0~/^Error/' file
Peter Nikitka
Honored Contributor

Re: how to remove content in a file

Hi,

even shorter as Sandman's solution:
awk '/^Error/' file

or taking '"Error" is the first word' into account:
awk '$1=="Error"' file

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arturo Galbiati
Esteemed Contributor

Re: how to remove content in a file

Hi,
sed -n '/^Error/p' file

HTH,
Art