Deleting rows starting with a character and word

Hi,

I have multiple files of same format and I want to delete the lines starting with # and The from all of them

I am using

 egrep -v '^(#|$)'

for # but unable to do for both # and The

Please guide
Thanks

try:

egrep -v '^[#$]' infile
1 Like
egrep -v '^(#|The)' infile

?

1 Like

I have used both the following and they are working, but I want to delete both # and The in one command.

 egrep -v '^(#|$)'  
 egrep -v '^(The|$)'
grep '^[^$|^#]' file
1 Like

try also:

egrep -v '^(#|The|[$])' infile

@bipinajith: Thanks. Its working but deleting lines starting with TER that I dont want.
@rdrtx1: Thanks its working fine.

we can use below sed command also

sed '/^[# The].*/d' $file
1 Like

This means: delete any line that starts with #, space, T, h, or e ..

This means: delete any line that starts with ^, $, | or # ..

1 Like

No. This grep looks for any line in file that starts with any character except for the characters $ , | , ^ , and # .

---------- Post updated at 00:37 ---------- Previous update was at 00:22 ----------

I don't understand why you have the |$ in these expressions.

If you want to remove lines starting with # and remove lines starting with The , you can do that with:

grep -Ev '^(#|The)'

The command:

grep -Ev '^(#|The|$)'

will remove any lines starting with # , lines starting with The , and empty lines.

---------- Post updated at 00:46 ---------- Previous update was at 00:37 ----------

The commands:

egrep -v '^(#|The|[$])'

and equivalently

grep -Ev '^(#|The|[$])'

will remove any line starting with # , any line starting with The , and any line starting with $ .

2 Likes

I tried to translate it into what the net effect would be. It effectively deletes (does not print) lines that start with those characters / it prints lines that do not start with these characters.
edit: I agree there would be a difference in case of empty lines.

1 Like
sed '/^[# The].*/d' $file 

it delete the lines stared with # and The.

like

File is....
#srinivas
The book
To Scrutinizer

Here it delete first two lines only

---------- Post updated at 04:19 AM ---------- Previous update was at 04:14 AM ----------

can you give me an example

1 Like

No it would leave only the first line

---------- Post updated at 04:19 AM ---------- Previous update was at 04:14 AM ----------

$ printf 'This\n\nis\n$a sample\n' 
This

is
$a sample
$ printf 'This\n\nis\n$a sample\n' | grep '^[^$|^#]'
This
is

vs.

$ printf 'This\n\nis\n$a sample\n' | grep -v '^[$|^#]'
This

is
1 Like