Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - remove all lines beginning with 'P'

Tags:

bash

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.

like image 293
user2450099 Avatar asked Jun 20 '13 08:06

user2450099


People also ask

How do I delete a range of lines in Linux?

The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.

How do you delete a line in bash?

# Deleting whole words ALT+Del Delete the word before (to the left of) the cursor ALT+d / ESC+d Delete the word after (to the right of) the cursor CTRL+w Cut the word before the cursor to the clipboard # Deleting parts of the line CTRL+k Cut the line after the cursor to the clipboard CTRL+u Cut/delete the line before ...

How do you delete a line in a script?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I get rid of lines above in vi?

Delete All Lines Press the Esc key to go to normal mode. Type %d and hit Enter to delete all the lines.


1 Answers

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
like image 136
bartimar Avatar answered Oct 25 '22 14:10

bartimar