I have a lot of lines in my LIST file and want to list only lines whose name does not start by (or contains) "git".
So far I have :
cat LIST | grep ^[^g]but I would like something like :
#not starting by "git"
cat LIST | grep ^[^(git)]
#not containing "git"
cat LIST | grep .*[^(git)].*but it is not correct. What regex should I use ?
5 Answers
Using grep in this case with -P option, which Interprets the PATTERN as a Perl regular expression
grep -P '^(?:(?!git).)*$' LISTRegular expression explanation:
^ the beginning of the string (?: group, but do not capture (0 or more times) (?! look ahead to see if there is not: git 'git' ) end of look-ahead . any character except \n )* end of grouping
$ before an optional \n, and the end of the stringUsing the find command
find . \! -iname "git*" 4 Since the OP is looking for a general regex and not specially for grep, this is the general regex for lines not starting with "git".
^(?!git).*
Breakdown:
^ beginning of line
(?!git) not followed by 'git'
.* followed by 0 or more characters
If you want to simply list all lines that don't contain git try this
cat LIST | grep -v git 3 Slight modification to the accepted answer that will work in more situations and a broader swath of engines (moves . to outside second closing parenthesis):
^(?:(?!git)).*$This example finds any lines that begin with anything except git or ls:
^(?:(?!git|ls)).*$ 1 If you wanna find the lines "not starting with git", you could also use:
^[^git].* 3