15 Practical Grep Command Examples in Linux / Unix: Home Free Ebook Start Here Contact About
15 Practical Grep Command Examples in Linux / Unix: Home Free Ebook Start Here Contact About
Home
Free eBook
Start Here
Contact
About
15 Practical Grep Command Examples In
Linux / UNIX
by SathiyaMoorthy on March 26, 2009
Tweet Tweet 84
Photo courtesy of Alexmes
You should get a grip on the Linux grep command.
This is part of the on-going 15 Examples series, where 15 detailed examples will be provided for a
specic command or functionality. Earlier we discussed 15 practical examples for Linux nd
command, Linux command line history and mysqladmin command.
In this article let us review 15 practical examples of Linux grep command that will be very useful to
both newbies and experts.
First create the following demo_le that will be used in the examples below to demonstrate grep
command.
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
1 of 30 23/04/14 16:42
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
1. Search for the given string in a single le
The basic usage of grep command is to search for a specic string in the specied le as shown
below.
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
2. Checking for the given string in multiple les.
Syntax:
grep "string" FILE_PATTERN
This is also a basic usage of grep command. For this example, let us copy the demo_le to
demo_le1. The grep output will also include the le name in front of the line that matched the
specic pattern as shown below. When the Linux shell sees the meta character, it does the expansion
and gives all the les as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively.
So it matches all the words such as the, THE and The case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4. Match regular expression in les
Syntax:
grep "REGEX" filename
This is a very powerful feature, if you can use use regular expression eectively. In the following
example, it searches for all the pattern that starts with lines and ends with empty with anything
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
2 of 30 23/04/14 16:42
in-between. i.e To search lines[anything in-between]empty in the demo_le.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition
operators:
? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.
5. Checking for full words, not for sub-strings using grep -w
If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out
a normal search will show out all the lines.
The following example is the regular grep where it is searching for is. When you search for is,
without any option it will show out is, his, this and everything which has the substring is.
$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The following example is the WORD grep where it is searching only for the word is. Please note
that this output does not contain the line This Line Has All Its First Character Of The Word With
Upper Case, even though is is there in the This, as the following is looking only for the word
is and not for this.
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6. Displaying lines before/after/around the match using grep -A, -B and -C
When doing a grep on a huge le, it may be useful to see some lines after the match. You might feel
handy if grep can show you not only the matching lines but also the lines after/before/around the
match.
Please create the following demo_text le for this example.
$ cat demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
* e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
3 of 30 23/04/14 16:42
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.1 Display N lines after match
-A is the option which prints the specied N lines after the match as shown below.
Syntax:
grep -A <N> "string" FILENAME
The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 Display N lines before match
-B is the option which prints the specied N lines before the match.
Syntax:
grep -B <N> "string" FILENAME
When you had option to show the N lines after match, you have the -B option for the opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
6.3 Display N lines around match
-C is the option which prints the specied N lines before the match. In some occasion you might
want the match to be appeared with the lines from both the side. This options shows N lines in both
the side(before & after) of match.
$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
7. Highlighting the search using GREP_OPTIONS
As grep prints out lines from the le by the pattern / string you had given, if you wanted it to
highlight which part matches the line, then you need to follow the following way.
When you do the following export you will get the highlighting of the matched searches. In the
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
4 of 30 23/04/14 16:42
following example, it will highlight all the this when you set the GREP_OPTIONS environment
variable as shown below.
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
8. Searching in all les recursively using grep -r
When you want to search in all the les under the current directory and its sub directory. -r option is
the one which you need to use. The following example will look for the string ramesh in all the les
in the current directory and all its subdirectory.
$ grep -r "ramesh" *
9. Invert match using grep -v
You had dierent options to show the lines matched, to show the lines before match, and to show the
lines after match, and to highlight match. So denitely Youd also want the option -v to do invert
match.
When you want to display the lines which does not matches the given string/pattern, use the option
-v as shown below. This example will display all the lines that did not match the word go.
$ grep -v "go" demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
10. display the lines which does not matches all the given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11. Counting the number of matches using grep -c
When you want to count that how many lines matches the given pattern/string, then use the option
-c.
Syntax:
grep -c "pattern" filename
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
5 of 30 23/04/14 16:42
$ grep -c "go" demo_text
6
When you want do nd out how many lines matches the pattern
$ grep -c this demo_file
3
When you want do nd out how many lines that does not match the pattern
$ grep -v -c this demo_file
4
12. Display only the le names which matches the given pattern using grep -l
If you want the grep to show out only the le names which matched the given pattern, use the -l
(lower-case L) option.
When you give multiple les to the grep as input, it displays the names of le which contains the text
that matches the pattern, will be very handy when you try to nd some notes in your whole directory
structure.
$ grep -l this demo_*
demo_file
demo_file1
13. Show only the matched string
By default grep will show the line which matches the given pattern/string, but if you want the grep
to show out only the matched string of the pattern then use the -o option.
It might not be that much useful when you give the string straight forward. But it becomes very
useful when you give a regex pattern and trying to see what it matches as
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14. Show the position of match in the line
When you want grep to show the position where it matches the pattern in the le, use the following
options as
Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345
$ grep -o -b "3" temp-file.txt
2:3
8:3
Note: The output of the grep command above is not the position in the line, it is byte oset of the
whole le.
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
6 of 30 23/04/14 16:42
97 Tweet Tweet 84 136 Like Like
15. Show line number while displaying the output using grep -n
To show the line number of le with the line matched. It does 1-based line numbering for each le.
Use -n option to utilize this feature.
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.
Additional Grep Tutorials
7 Linux Grep OR, Grep AND, Grep NOT Operator Examples
Regular Expressions in Grep Command with 10 Examples Part I
Advanced Regular Expressions in Grep Command with 10 Examples Part II
Search in a *.bz2 le using bzgrep, and *.gz le using zgrep
Awesome Linux Articles
Following are few awesome 15 examples articles that you might nd helpful.
Linux Crontab: 15 Awesome Cron Job Examples
Mommy, I found it! 15 Practical Linux Find Command Examples
15 Examples To Master Linux Command Line History
Unix LS Command: 15 Practical Examples
> Add your comment
Linux provides several powerful administrative tools and utilities which will
help you to manage your systems eectively. If you dont know what these tools are and how to use
them, you could be spending lot of time trying to perform even the basic administrative tasks. The
focus of this course is to help you understand system administration tools, which will help you to
become an eective Linux system administrator.
Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
50 Linux Sysadmin Tutorials 1.
50 Most Frequently Used Linux Commands
(With Examples)
2.
Top 25 Best Linux Performance Monitoring and
Debugging Tools
3.
Mommy, I found it! 15 Practical Linux Find
Command Examples
4.
Awk Introduction 7 Awk Print Examples
Advanced Sed Substitution Examples
8 Essential Vim Editor Navigation
Fundamentals
25 Most Frequently Used Linux IPTables
Rules Examples
Turbocharge PuTTY with 12 Powerful
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
7 of 30 23/04/14 16:42
Linux 101 Hacks 2nd Edition eBook 5. Add-Ons
Tags: File Search Utility, Grep Command, Highlight Search Output, Linux Full-Text Searching, Linux
Grep Command, Search File Content, Search Multiple Files
{ 104 comments read them below or add one }
1 Joao Trindade March 28, 2009 at 3:54 am
You have a small glitch:
>> 4. Match regular expression in les using grep -i
Dont you mean:
4. Match regular expression in les using grep -e
The rest of the post is great.
2 Ramesh March 29, 2009 at 12:16 am
Joao,
Thanks for pointing it out. I have corrected it. Also, we can do REGEX without the option -e as
shown in the example #4.
From Man Pages:
SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.
3 dragon March 31, 2009 at 11:26 pm
Hi:
FYI, tip 14 will be
2:3
8:3
on Ubuntu system. (including the \n character I guess
4 Ramesh March 31, 2009 at 11:44 pm
Dragon,
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
8 of 30 23/04/14 16:42
Thanks for pointing it out. Ive corrected it.
5 Francesco Talamona April 26, 2009 at 2:48 am
I nd very useful the following command, when you have to deal with a very lengthy
conguration le full of comments:
grep -v -E ^\#|^$ /etc/squid/squid.conf
It skips every line beginning with an hash (#) or empty, so you can see at a glance the 15 lines
edited out of a +4400 lines text le.
BTW interesting topics, great posts
6 albar May 7, 2009 at 7:51 pm
help me
how to bzgrep : ^C02
but ^C is count as one special character,
in this word:
data1^C02data2
thanks
7 Ramesh Natarajan May 8, 2009 at 5:51 pm
@Francesco Talamona,
Thanks a lot for sharing your grep command example. Yes. all those empty lines and comment
lines can get very annoying when you do grep. So, it is an excellent idea to hide them in the
grep output with your examples.
8 sasikala May 11, 2009 at 9:41 pm
@albar,
try like this
grep \^C02
9 albar May 12, 2009 at 1:18 am
@sasikala ,
i do have try that too, but still got nothing,
but it works when ^ and C count as two character
thanks
10 SathiyaMoorthy May 12, 2009 at 4:33 am
@albar
You should type ^C as ctrl-v + ctrl-c in grep as single character as
$ grep ^C02 le
Dont escape, dont type it as ^ C as two characters. Hope this helps.
11 albar May 12, 2009 at 8:59 pm
@sathiya,
god bless u all
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
9 of 30 23/04/14 16:42
it works thanks
12 Manish Patel May 21, 2009 at 7:00 pm
Hi
I am trying to exclude the last word of all the line like sync.php, uploads.php, backup.php
File text include as below
/usr/home/htdocs/drag-and-drop/htdocs.php
/usr/home//htdocs/sms/publish/pages/sync.php
/usr/home/htdocs/track/backup.php
/usr/home/htdocs/smstest/smstest.php
/usr/home/htdocs/uploads.php
/usr/home/htdocs/017/backup.php
How can I achieve that using grep or sed or awk
Also how I can use * wildcard in sed command like to replace *.php to *.txt or any other
extension.
Thank you in advance.
Manish
13 Francesco Talamona May 21, 2009 at 10:36 pm
Are you restricted to sed or awk?
1)
dirname /usr/home/htdocs/drag-and-drop/htdocs.php
/usr/home/htdocs/drag-and-drop
2)
rename does what you want
14 Manish Patel May 24, 2009 at 6:55 pm
Hi,
Those lines are the contents of the text le and I dont want to change the actual directory or
the le on server. I want to change the contents of the le where all le le names ending at the
line should be removed. So the nal le contents should look like this
cat lecontenet.txt
/usr/home/htdocs/drag-and-drop/
/usr/home//htdocs/sms/publish/pages/
/usr/home/htdocs/track/
/usr/home/htdocs/smstest/
/usr/home/htdocs/
/usr/home/htdocs/
I think rename would not help here in editing le contents.
Thank you
Manish
15 SathiyaMoorthy May 24, 2009 at 11:43 pm
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
10 of 30 23/04/14 16:42
rev lecontenet.txt | cut -d/ -f2- | rev
rev lecontenet.txt > reverses the le and pipes to cut command.
cut -d/ -f2- > cuts o the rst eld ( cuts o last eld, as it is reversed ).
rev > prints the output given order.
16 P0B0T May 26, 2009 at 11:36 pm
Manish,
I believe youre looking for the following
sed -e s/.php$// lecontenet.txt
17 P0B0T May 26, 2009 at 11:39 pm
Sorry, didnt read your requirement carefully.
Try this:
sed -e s/\/[^/]*.php$/\// lecontenet.txt
18 Manish Patel June 5, 2009 at 5:31 pm
Hi
Thank you to Sathiya Moorthy and P0B0T.
Both solution worked very nicely for me.
P0B0T can you explain how your command works for each dened option s/\/[^/]*.php$/\//
Thank you
Manish
19 mano June 10, 2009 at 3:00 am
The above info on grep is really great. I want to search for a string in all the les in the
directory and add a $ symbol at the start of the searched line and save in the same le.
20 SathiyaMoorthy June 18, 2009 at 10:49 pm
@mano
More than using grep for this requirement, you can use sed which is:
sed -i s/.*abc.*/$&/ *
-i : edit the input le.
s/// : substitute the matched pattern with the replacement string.
/.*abc.*/ : match the string abc
/$&/ : Replace with $ followed by matched string.
* : all the les in the current directory.
This is one way of satisfying your requirement, there may be other ecient ways.
Hope this helps.
21 mano June 19, 2009 at 12:17 am
Hi SathiaMoorthy, Thank u so much. it works ne. If I need to search for les in all
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
11 of 30 23/04/14 16:42
subdirectories, how should this sed command modied?
Thanks in advance.
mano
22 SathiyaMoorthy June 27, 2009 at 12:05 am
@mano
Modication in sed command is not needed.
To search for all les in the subdirectory.
nd . -type f
Execute the command on all those les with -exec.
nd . -type f -exec sed -i s/.*abc.*/#&/ {} \;
But think twice before executing this command, because it will recursively edit all the les.
Taking backup before executing this command is wise.
Refer the earlier article linux nd command examples.
23 Vidya July 1, 2009 at 2:59 am
Hi,
I want to grep next 3 words in a line from the matching criteria word..
like if the line is
This is -g gateway -e enterprise -s server
Then I want to grep -g gateway -e enterprise from the line
Can you please help me in this case.
Here gateway and enterprise value can be anything so need to grep next 3 words starting form
-g
24 SathiyaMoorthy July 1, 2009 at 5:58 am
@Vidhya
$ grep -o -E -g \w+ -e \w+ FILENAME
-g gateway -e enterprise
Explanation of the above command.,
-o : only matching ( point 13. )
-E : extended regexp
: indicate end of options
\w+ : word
25 Vidya July 1, 2009 at 9:00 am
Hi Sathiya,
Its not working.
It says
grep: illegal option o
grep: illegal option E
Usage: grep -hblcnsviw pattern le . . .
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
12 of 30 23/04/14 16:42
I am working on Solaris and setting shell as bash.
26 Amit Agarwal September 21, 2009 at 6:18 am
grep version on solaris is little older and as man would show you all these options are not
available, so you can try ack (standalone) version which is very powerful and requires only perl
to be installed.
27 learner October 7, 2009 at 5:31 am
Hi,
How to use grep to nd lines containing multiple strings
ex: line1:Today is oct 7, wednesday. not 8th
line2: This is not summer.
line3: when is summer?
I want to return line2 containing strings not and summer both.
Thank You.
28 SathiyaMoorthy October 7, 2009 at 10:41 am
@learner
There are several ways possible, use the one which you nd as appropriate.
$ grep "not.*summer" file1
line2: This is not summer.
$ grep "not" file1 | grep "summer"
line2: This is not summer.
29 learner October 7, 2009 at 10:56 pm
@SathiyaMoorthy
Thank You for your very quick reply.
My question was not piping and hard coding every string , as i mentioned multiple strings, i
was looking for something in likes of
grep -F string1
string2
string3
string4
..
stringn lename
which returns single occurrence of something like either string1 ,string2,.. stringn or all ..,
what i wanted was only string1 and string2 and . stringn begin returned.
[please note that i will be provided with strings as newline separated strings ,which i don't want
to parse again and i have constraint of using grep only]
Thank You.
30 Ashish December 1, 2009 at 4:07 pm
Hi,
I need to sthing like this
I have a le containing 400 domainId values seprated by new line
ex. domain.txt
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
13 of 30 23/04/14 16:42
domain1
domain2
domain3
I have a script that takes each domain and calls an api that returns me an xml.
like this for each domain
val1
domain1
val2
val3
val4
XXX
val1
now i want to spit out the domain name in a le that does not matches domainid value XXX.
how can i do it using grep
TIA
31 Ashish December 1, 2009 at 4:16 pm
Hi,
I need to sthing like this
I have a le containing 400 domainId values seprated by new line
ex. domain.txt
domain1
domain2
domain3
I have a script that takes each domain and calls an api that returns me an xml.
like this for each domain
<tag1>val1</tag1>
<domain>domain1</domain>
<tag2>val2</tag2>
<tag3>val3</tag3>
<tag4>val4</tag4>
<domainid>XXX</domainid>
<tag5>val1</tag5>
now i want to spit out the domain name in a le that does not matches domainid value XXX.
how can i do it using grep
TIA
32 Varun December 17, 2009 at 7:16 am
Hi,
The options mentioned in point 6 for displaying the context with A, B, & C does not seem to
work on Solaris 10 with both grep & egrep
Is there a version of this grep available for Solaris?
Thank you,
Varun.
33 Jawn Hewz December 21, 2009 at 7:54 pm
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
14 of 30 23/04/14 16:42
Does the -b (byte oset) work when greping binary les? I do not get an oset returned when I
grep a binary le, but I do when using a text le. I am using grep under Cygwin.
34 fety January 11, 2010 at 3:32 am
thanks very much for this tutorial. it is very helpful..
35 eMancu January 24, 2010 at 12:49 pm
Awsome tutorial!
Im reading all your blog, its amazing!
36 Raghu Baba January 30, 2010 at 4:44 am
Hai.. I want to Parse my le .. Word to Excel .. so tell me some grep & cut commands
37 Je Floyd February 1, 2010 at 6:54 pm
Whats the dierence between $ grep -c ill memo and $ grep -n ill memo?
38 joeq February 4, 2010 at 3:57 am
hi
i got 1 problemhow can i nd a numbers like 99,000,000.95 in my server database using unix
command..
tq
39 abhishek February 21, 2010 at 4:31 am
content was very useful
40 Anonymous March 8, 2010 at 4:26 am
Hi,
Those lines are the contents of the text le and I dont want to change the actual directory or
the le on server. I want to change the contents of the le where all le le names ending at the
line should be removed. So the nal le contents should look like this
cat lecontenet.txt
/usr/home/htdocs/drag-and-drop/
/usr/home//htdocs/sms/publish/pages/
/usr/home/htdocs/track/
/usr/home/htdocs/smstest/
/usr/home/htdocs/
/usr/home/htdocs/
I think rename would not help here in editing le contents.
for this question , awk really helpful with single line command
go to the current directory
ls -l | grep -v ^d | awk {print $9} > new.txt
$9 is the last led which is lename only when u list with option ls -l ,
new.txt contains only the lenames which you wnated to lter out
41 skipper March 26, 2010 at 5:54 am
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
15 of 30 23/04/14 16:42
nice article
42 VIKAS April 4, 2010 at 7:23 pm
Excellent stu, just loved grep -A,B,C options
and
grep -o xxxx.*yyyy kinda commands.
This will help me a lot, I used awk more in my shell scripts, But I have got a new friend grep
for some selective printing
43 Sam April 17, 2010 at 3:35 am
I have just nish reading this wonderful article. Let me answer this:
Whats the dierence between $ grep -c ill memo and $ grep -n ill memo?
grep -c return the number lines that matched ill in memo.
grep -n return the matched lines with line-number as prex.
44 Chong April 20, 2010 at 3:05 am
how to grep a statement contain * from a le at the same time to match the rst character too.
example the statement in lename prole.txt :-
Mary stay at uttana*istana with her grandmum
current grep statement :-
grep ^Mary stay at uttana*istana prole.txt
result: no row matched the grep statement because of *
How to use grep command for the combine condition of statement with * and match the front
word?
45 vm April 30, 2010 at 8:24 pm
In bash script without using perl, how i can grep a number from a le if there exists a number
greater than 80 in that le.
46 palash June 13, 2010 at 12:14 pm
grep -c pattern lename returns the number of lines that matches the pattern, even if the
pattern occurred for more than one time in any line. Is there any option to know how many
times the pattern matched in a le?
47 mathan June 20, 2010 at 10:21 am
HI,
I am new to linux
can you tell me how to exit from grep command.
mistakenly i type grep lename
But its nothing shown. pls looking for quick reply
48 SathiyaMoorthy July 4, 2010 at 7:20 am
@mathan
There is nothing like exiting from grep.
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
16 of 30 23/04/14 16:42
First argument to grep is taken as PATTERN, not as lename. So as far as i understand it is
waiting for input to match. So just exit from it using CTRL+D.
49 Ross Huggett September 16, 2010 at 5:04 am
Nice article. Thanks.
50 ec October 6, 2010 at 6:14 pm
Hi to all,
I just started to learn linux a month ago
Can I extract 2 to 6 letter words from a text le using one grep command only!
To mention that each word is on its own line
whats the grep command to do this job?
I tried any combination of grep and not the result which I am looking for
51 Lou February 24, 2011 at 1:16 pm
Is there a way to grep for a word on in a le and return that line plus the next?
52 Francesco Talamona February 26, 2011 at 3:33 am
@ Lou:
cat testle.txt
rst line
matching line
following line
ending line
grep matching -B 1 testle.txt
rst line
matching line
53 abhishek kumar April 19, 2011 at 12:53 pm
really to nice and too simple to understand,
thats great
thank you
54 Nikita April 21, 2011 at 12:06 pm
PLEASE HELP ON QUESTION B.
You are searching a le for lines that contain US state abbreviations in parentheses. e.g.:
(ma),(NH),(Ky), etc. So you decide to match any line containing ( ) with exactly two characters
(not letters) in between.
A) What grep will get this done?
My Answer> grep ([a-zA-Z][A-Za-z]) le
You now notice that some of the lines that the grep from part A matched contain the the string
(expired). You want to eliminate these lines from your output, so you decide to pipe your output
to another grep.
B) What will the new command be? (both greps with the pipe)
My Answer > grep v grep | grep ([a-zA-Z] [A-Za-z]) -> PLEASE HELP!
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
17 of 30 23/04/14 16:42
55 Francesco Talamona April 22, 2011 at 12:44 pm
@Nikita:
One step is enough:
egrep \([a-zA-Z]{2}\) le
56 suprabhat April 27, 2011 at 3:26 am
How to display all lines that have less than 9 character in a le
57 Paul May 16, 2011 at 3:09 pm
Im new to linux; was wondering what does \# after the grep command accomplish, as shown in
the example below?
grep \# input*.txt | awk {print $4} | sort | uniq > output.txt
thank you
58 Dinesh May 17, 2011 at 4:03 pm
For a given patern like
Fri Nov 26 16:04:52 2010
I want to grep for all the lines in a le having the above format.
But I have all the values except for the time, that is 16:04:52 , the data I have is
Fri Nov 26 2010 . The le is having 5 years of date with the timestamp as specied above.
Please let me know How shall I grep the le to get all the lines on the date Fri Nov 26 2010 .
thanks
59 shyam May 29, 2011 at 10:45 pm
i have a doubt here i tried to look at output of cmd
grep [^A-Z] le.txt
this is showing all characters excluding capital letter
what does this command actually do
60 shivaraj Patil July 25, 2011 at 11:14 am
HI i have a le with this values
100 rst line
101 second line
101
102
102
109
now i need a script that can take two lines and nd which is greatest
61 sudheer September 10, 2011 at 8:32 pm
1) Use grep (or awk) to output all lines in a given le which contain employee ID numbers.
Assume that each employee ID number consists of 1-4 digits followed by two letters: the rst is
either a W or a S and the second is either a C or a T. ID numbers never start with 0s. Further
assume that an employee ID is always proceeded by some type of white space tab, blank, new
line etc. However, there might be characters after it, for example punctuation.
What to turn in: Turn in three things:
15 Practical Grep Command Examples In Linux /... http://www.thegeekstu.com/2009/03/15-practica...
18 of 30 23/04/14 16:42
a. A le with the regular expression which can directly be used by grep (or awk)
b. A text le which you used to test your regular expression. Make sure that you include valid
and invalid employee IDs, have them at the beginning and the end of lines, sentences, etc.
c. A second document which re-writes the regular expression in a more human-readable form
and explains the purpose of the dierent components of the regular expression. Also include a
short explanation of your test cases.
2) Use grep (or awk) to output all the lines in a given le which contain a decimal number (e.g.
a number which includes a decimal point). Decimal numbers do not have leading zeros but they
might have trailing zeros. Assume the number is always surrounded by white space.
What to turn in: The same three things as above (except, of course, for this problem).
3) Write a regular expression for the valid identiers in Java. You are allowed to use shortcuts,
but need to make sure that you specify exactly what they are (e.g. if you use digit specify that
that means 0, 1, 2, 3, .9.)
62 Dinesh September 22, 2011 at 12:43 pm
Paul
grep \# input*.txt | awk {print $4} | sort | uniq > output.txt
Since # is a special character,we are treating # as # by putting backslash infront of that.
Noe Greap searches for pattern # in a list of le starting as input and nding a txt and then awk
prints the 4th eld and sort is doing sorting the 4th eld returns from awk and unis is doing
uniq operation.
63 Dinesh September 22, 2011 at 12:45 pm
Shyam
grep [^A-Z] le.txt
Grep will print the lines that does not start with CAPTIAL LETTERS.
Using ^ inside the [] will do the work opposite to the pattern what you have been searching for