Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep: Invalid regular expression

I have a text file which looks like this:

haha1,haha2,haha3,haha4
test1,test2,test3,test4,[offline],test5
letter1,letter2,letter3,letter4
output1,output2,[offline],output3,output4
check1,[core],check2
num1,num2,num3,num4

I need to exclude all those lines that have "[ ]" and output them to another file without all those lines that have "[ ]".

I'm currently using this command:

grep ",[" loaded.txt | wc -l > newloaded.txt

But it's giving me an error:

grep: Invalid regular expression

like image 957
tuturyokgaming Avatar asked Apr 29 '15 01:04

tuturyokgaming


People also ask

How do you grep a regular expression?

Grep Regular Expression In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

How do you grep with wildcards?

The wildcard * (asterisk) can be a substitute for any number of letters, numbers, or characters. Note that the asterisk (*) works differently in grep. In grep the asterisk only matches multiples of the preceding character. The wildcard * can be a substitute for any number of letters, numbers, or characters.

How do I grep a pattern in Linux?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

How do you grep a character?

Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ). finds any line in the file list starting with "b." displays any line in list where "b" is the only character on the line.


1 Answers

Use grep -F to treat the search pattern as a fixed string. You could also replace wc -l with grep -c.

grep -cF ",[" loaded.txt > newloaded.txt

If you're curious, [ is a special character. If you don't use -F then you'll need to escape it with a backslash.

grep -c ",\[" loaded.txt > newloaded.txt

By the way, I'm not sure why you're using wc -l anyways...? From your problem description, it sounds like grep -v might be more appropriate. -v inverts grep's normal output, printing lines that don't match.

grep -vF ",[" loaded.txt > newloaded.txt
like image 51
John Kugelman Avatar answered Nov 15 '22 07:11

John Kugelman