I have a text file that\'s about 300KB in size. I want to remove all lines from this file that begin with the letter \"P\". This is what I\'ve been using:
&g
^ to anchor your pattern to the beginning of the line ;sed and the d flag.cat file.txt | sed '/^P/d'
Use sed-only:
sed '/^P/d' file.txt > new.txt
Use start of line mark and quotes:
cat file.txt | egrep -v '^P.*'
P* means P zero or more times so together with -v gives you no lines
^P.* means start of line, then P, and any char zero or more times
Quoting is needed to prevent shell expansion.
This can be shortened to
egrep -v ^P file.txt
because .* is not needed, therefore quoting is not needed and egrep can read data from file.
As we don't use extended regular expressions grep will also work fine
grep -v ^P file.txt
Finally
grep -v ^P file.txt > new.txt
This works:
cat file.txt | egrep -v -e '^P'
-e indicates expression.
Use sed with inplace substitution (for GNU sed, will also for your cygwin)
sed -i '/^P/d' file.txt
BSD (Mac) sed
sed -i '' '/^P/d' file.txt
With awk:
awk '!/^P/' file.txt
! (negation), that negates the following pattern ;
/^P/ means "match all lines starting with a capital P", P".awk's behavior when { … } (action block) is missing, that is to print the record validating the condition.So, to rephrase, it ignores lines starting with a capital P and print everything else.
sed is line oriented and awk column oriented. For your case you should use the first one, see Edouard Lopez's reponse.