简体   繁体   中英

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

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

An alternative method to Grep

It's unclear if you want to remove lines that might contain either bracket [] , or only the ones where the brackets specifically surround characters. Regardless of which method you intend to use, sed can easily remove lines that fit a definitive pattern:

To delete only lines that contained both brackets surrounding characters [...] :

sed '/\[.*\]/d' loaded.txt > newloaded.txt

Another approach might be to remove any line that contained either bracket:

sed '/\[/d;/\]/d' loaded.txt > newloaded.txt

(eg. lines containing either [ or ] would be deleted)

Your grep command doesn't seem to be excluding anything. Also, why are you using wc ? I thought you want the lines, not their count.

So if you just want the lines, as you say, that don't have [] , then this should work:

grep -v "\[" loaded.txt > new.txt

You can also use awk for this:

awk -F\[ 'NF==1' file > newfile
cat newfile
haha1,haha2,haha3,haha4
letter1,letter2,letter3,letter4
num1,num2,num3,num4

Or this:

awk '!/\[/' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM