This article will explain grep one liners which are very useful in day to day activities.
. Match a single character of any value, except end of line
^ Represent start of the line.
$ Represent the end of the line.
[ ] Match any one of the enclosed characters
[^ ] Match any one character except those enclosed in [ ]
[a-z] Match Any Character between a to z (Lower Case)
[0-9] Match Any Number between 0 to 9
[A-Z] Match Any Character between A to Z (Upper Case)
Let see how to use these expression in a simple way.
1. How to find empty lines in a file.
$ grep "^$" F_Data_File.txt
^ represent start of the line & $ represent the end of the line.
2. List all the lines other than empty lines.
$ grep -v "^$" F_Data_File.txt
-v negate the search. "^$" will list the empty lines and -v force greap to select lines which are not empty.
3. List down all the directories from current directory.
$ ls -ltr | grep "^d"
4. List down all the files from current directory.
$ ls -ltr | grep "^-"
5. Lines with either Unix or unix <Remember [] Match any one of the enclosed characters>
$ grep '[Uu]nix'
$ grep '[uU]nix'
6. Line with at least one letter irrespective of upper or lower.
$ grep '[A-Z]' <With at least one letter in Upper case>
$ grep '[a-z]' <With at least one letter in Lower case>
$ grep '[a-zA-Z]' <With at least one letter irrespective of upper or lower>
7. Anything not a letter or number <Remember [^] Match any one character except those enclosed in [ ]>
$ grep '[^a-zA-Z0-9]
8. Lines with exactly one character
$ grep '^.$'
$ grep '^..$' <Will match for exactly 2 character>
9. Lines which are starting with .(dot) and any character
$ echo ".Unix is awesome" | grep "^\.[a-zA-Z]"
Above will return true. ^ represent start of the line while \ used to escape the .(dot).
$ echo "Unix is awesome. Learn it" | grep "\.[a-zA-Z]"Will not return
anything as there is space not character after .(dot)
$ echo "Unix is awesome.Learn it" | grep "\.[a-zA-Z]"
Will return true.
Any line starting with .(dot) and followed by a Number.
$ echo ".192.162.166" | grep "^\.[0-9]"
Will return true.
10. Validate date format means it should have DD-MM-YYYY format.
$ echo "01-01-1990" | grep '[0-9][0-9][-][0-9][0-9][-][0-9][0-9][0-9][0-9]$'
It seems quite tedious to write the pattern again and again, we can over come by writing the number of instance we need to match as given below.
$ echo "01-01-1990" | grep '[0-9]\{2\}[-][0-9]\{2\}[-][0-9]\{4\}$'
. Match a single character of any value, except end of line
^ Represent start of the line.
$ Represent the end of the line.
[ ] Match any one of the enclosed characters
[^ ] Match any one character except those enclosed in [ ]
[a-z] Match Any Character between a to z (Lower Case)
[0-9] Match Any Number between 0 to 9
[A-Z] Match Any Character between A to Z (Upper Case)
Let see how to use these expression in a simple way.
1. How to find empty lines in a file.
$ grep "^$" F_Data_File.txt
^ represent start of the line & $ represent the end of the line.
2. List all the lines other than empty lines.
$ grep -v "^$" F_Data_File.txt
-v negate the search. "^$" will list the empty lines and -v force greap to select lines which are not empty.
3. List down all the directories from current directory.
$ ls -ltr | grep "^d"
4. List down all the files from current directory.
$ ls -ltr | grep "^-"
5. Lines with either Unix or unix <Remember [] Match any one of the enclosed characters>
$ grep '[Uu]nix'
$ grep '[uU]nix'
6. Line with at least one letter irrespective of upper or lower.
$ grep '[A-Z]' <With at least one letter in Upper case>
$ grep '[a-z]' <With at least one letter in Lower case>
$ grep '[a-zA-Z]' <With at least one letter irrespective of upper or lower>
7. Anything not a letter or number <Remember [^] Match any one character except those enclosed in [ ]>
$ grep '[^a-zA-Z0-9]
8. Lines with exactly one character
$ grep '^.$'
$ grep '^..$' <Will match for exactly 2 character>
9. Lines which are starting with .(dot) and any character
$ echo ".Unix is awesome" | grep "^\.[a-zA-Z]"
Above will return true. ^ represent start of the line while \ used to escape the .(dot).
$ echo "Unix is awesome. Learn it" | grep "\.[a-zA-Z]"Will not return
anything as there is space not character after .(dot)
$ echo "Unix is awesome.Learn it" | grep "\.[a-zA-Z]"
Will return true.
Any line starting with .(dot) and followed by a Number.
$ echo ".192.162.166" | grep "^\.[0-9]"
Will return true.
10. Validate date format means it should have DD-MM-YYYY format.
$ echo "01-01-1990" | grep '[0-9][0-9][-][0-9][0-9][-][0-9][0-9][0-9][0-9]$'
It seems quite tedious to write the pattern again and again, we can over come by writing the number of instance we need to match as given below.
$ echo "01-01-1990" | grep '[0-9]\{2\}[-][0-9]\{2\}[-][0-9]\{4\}$'
No comments:
Post a Comment