9

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:

> cat file.txt | egrep -v P*

That isn't outputting to console. I can use cat on the file without another other commands and it prints out fine. My final intention being to:

> cat file.txt | egrep -v P* > new.txt

No error appears, it just doesn't print anything out and if I run the 2nd command, new.txt is empty. I should say I'm running Windows 7 with Cygwin installed.

user2450099
  • 375
  • 3
  • 7
  • 16

5 Answers5

16

Explanation

  1. use ^ to anchor your pattern to the beginning of the line ;
  2. delete lines matching the pattern using sed and the d flag.

Solution #1

cat file.txt | sed '/^P/d'

Better solution

Use sed-only:

sed '/^P/d' file.txt > new.txt
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
10

With awk:

awk '!/^P/' file.txt

Explanation

  1. The condition starts with an ! (negation), that negates the following pattern ;
    • /^P/ means "match all lines starting with a capital P",
  2. So, the pattern is negated to "ignore lines starting with a capital P".
  3. Finally, it leverage 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.

Note

sed is line oriented and awk column oriented. For your case you should use the first one, see Edouard Lopez's reponse.

Zulu
  • 8,765
  • 9
  • 49
  • 56
  • As a toy use case for `awk`, say we want to print `: ` in all uncommented lines (i.e., not starting with `#`). Here we're actually operating on fields instead of just printing the full lines, so `awk` makes sense: `awk '!/^#/ {print $1": "$4}' file.txt` – tdy Jun 18 '21 at 04:36
2

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
Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
2

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
bartimar
  • 3,374
  • 3
  • 30
  • 51
1

This works:

cat file.txt | egrep -v -e '^P'

-e indicates expression.

Prasanth
  • 5,230
  • 2
  • 29
  • 61