How to use grep command In Linux / UNIX with examples

See all UNIX related articles/faq
How do I use grep command on Linux or Apple macOS/OS X? How can I use grep command on Unix operating systems? Can you give me a simple examples of the grep command?

Grep is an essential Linux and Unix command. It is used to search text and strings in a given file. In other words, grep command searches the given file for lines containing a match to the given strings or words. It is one of the most useful commands on Linux and Unix-like system for developers and sysadmins. Let us see how to use grep on a Linux or Unix like system.

Did you know?

The name, “grep”, derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:
g/re/p
The grep utilities are a family that includes grep, grep -E (formally egrep), and grep -F (formally fgrep) for searching files. For most use cases, fgrep is sufficient due to speed and only looking into strings and words. However, typing grep is easy. Hence, it is a personal choice.

grep command examples in Linux and Unix

Below is some standard grep command explained with examples to get you started with grep on Linux, macOS, and Unix:

  1. Search any line that contains the word in filename on Linux:
    grep 'word' filename
  2. Perform a case-insensitive search for the word ‘bar’ in Linux and Unix:
    grep -i 'bar' file1
  3. Look for all files in the current directory and in all of its subdirectories in Linux for the word ‘httpd’:
    grep -R 'httpd' .
  4. Search and display the total number of times that the string ‘nixcraft’ appears in a file named frontpage.md:
    grep -c 'nixcraft' frontpage.md

Let us see all commands and options in details.

Syntax

WARNING! The egrep and fgrep commands syntax have been deprecated since 2007. Beginning with GNU Grep version 3.8, calling these commands will now issue a warning to the user that instead, they should use grep -E and grep -F, syntax, respectively.

The syntax is as follows:

grep 'word' filename
 
# Interpret PATTERNS as fixed strings, not regular expressions (regex) when fgrep used.
grep -F 'word-to-search' file.txt
grep -F 'pattern' filename
 
grep 'word' file1 file2 file3
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName
grep [-options] pattern filename
grep -F [-options] words file

How do I use grep to search a file on Linux?

Search the /etc/passswd file for a user named ‘boo’, enter:
$ grep boo /etc/passwd
Sample outputs:

foo:x:1000:1000:boo,,,:/home/boo:/bin/ksh

We can use fgrep/grep to find all the lines of a file that contain a particular word. For example, to list all the lines of a file named address.txt in the current directory that contain the word “California”, run:
$ grep -F California address.txt
Please note that the above command also returns lines where “California” is part of other words, such as “Californication” or “Californian”. Hence pass the -w option with the grep/fgrep command to get only lines where “California” is included as a whole word:
$ grep -F -w California address.txt
You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option. For instance, type the following command:
$ grep -i "boo" /etc/passwd

The last grep -i "boo" /etc/passwd can run as follows using the cat command too:
$ cat /etc/passwd | grep -i "boo"

How to use grep recursively

You can search recursively i.e. read all files under each directory for a string “192.168.1.5”
$ grep -r "192.168.1.5" /etc/
OR
$ grep -R "192.168.1.5" /etc/
Sample outputs:

/etc/ppp/options:# ms-wins 192.168.1.50
/etc/ppp/options:# ms-wins 192.168.1.51
/etc/NetworkManager/system-connections/Wired connection 1:addresses1=192.168.1.5;24;192.168.1.2;

You will see result for 192.168.1.5 on a separate line preceded by the name of the file (such as /etc/ppp/options) in which it was found. The inclusion of the file names in the output data can be suppressed by using the -h option as follows:
$ grep -h -R "192.168.1.5" /etc/
OR
$ grep -hR "192.168.1.5" /etc/
Sample outputs:

# ms-wins 192.168.1.50
# ms-wins 192.168.1.51
addresses1=192.168.1.5;24;192.168.1.2;

How to use grep to search words only

When you search for boo, grep will match fooboo, boo123, barfoo35 and more. You can force the grep command to select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" file

How to use grep to search 2 different words

Use the egrep command as follows:
$ grep -E -w 'word1|word2' /path/to/file
The -E option turns on extend regular expressions. Do not use the following deprecated syntax:
$ egrep -w 'word1|word2' /path/to/file

Ignore case

We can force grep to ignore case distinctions in patterns and data. For example, when I search for ‘bar’, match ‘BAR’, ‘Bar’, ‘BaR’ and so on:
$ grep -i 'bar' /path/to/file
In this example, I am going to include all subdirectories in a search:
$ grep -r -i 'main' ~/projects/

How can I count line when words has been matched

The grep can report the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
Pass the -n option to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'root' /etc/passwd

1:root:x:0:0:root:/root:/bin/bash
1042:rootdoor:x:0:0:rootdoor:/home/rootdoor:/bin/csh
3319:initrootapp:x:0:0:initrootapp:/home/initroot:/bin/ksh

Force grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
$ grep -v bar /path/to/file
$ grep -v '^root' /etc/passwd

Display lines before and after the match

Want to see the lines before your matches? Try passing the -B to the grep:
$ grep -B NUM "word" file
$ grep -B 3 "foo" file1

Similarly, display the lines after your matches by passing the -A to the grep:
$ grep -A NUM "string" /path/to/file
# Display 4 lines after dropped word matched in firewall log file #
$ grep -A 4 "dropped" /var/log/ufw.log

We can combine those two options to get most meaningful outputs:
$ grep -C 4 -B 5 -A 6 --color 'error-code' /var/log/httpd/access_log
Here is a sample shell script that fetches the Linux kernel download urls:

.......
...
_out="/tmp/out.$$"
curl -s https://www.kernel.org/ > "$_out"
#######################
## grep -A used here ##
#######################
url="$(grep -A 2 '<td id="latest_button">' ${_out}  | grep -Eo '(http|https)://[^/"]+.*xz')"
gpgurl="${url/tar.xz/tar.sign}"
notify-send "A new kernel version ($remote) has been released."
echo "* Downloading the Linux kernel (new version) ..."
wget -qc "$url" -O "${dldir}/${file}"
wget -qc "$gpgurl" -O "${dldir}/${gpgurl##*/}"
.....
..

UNIX / Linux pipes

grep command often used with shell pipes. In this example, show the name of the hard disk devices:
# dmesg | grep -E '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
However, above command can be also used as follows without shell pipe:
# grep -i 'Model' /proc/cpuinfo

model		: 30
model name	: Intel(R) Core(TM) i7 CPU       Q 820  @ 1.73GHz
model		: 30
model name	: Intel(R) Core(TM) i7 CPU       Q 820  @ 1.73GHz

One of my favorite usage of grep or egrep command to filter the output of the yum command/dpkg command/apt command/apt-get command:
# dpkg --list | grep linux-image
# yum search php | grep gd
# apt search maria | grep -E 'server|client'

Linux grep commands explained with shell pipes examples

How do I list just the names of matching files?

Use the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
OR
$ grep -Rl 'main' /path/to/project/dir/

Colors option

Finally, we can force grep to display output in colors, enter:
$ grep --color vivek /etc/passwd

Grep command in action

In conclusion, the --color option increase readability. For example, the GREP_COLOR environment variable and the grep --color=always can be used as follows:
GREP_COLOR='1;35' grep --color=always 'vivek' /etc/passwd
GREP_COLOR='1;32' grep --color=always 'vivek' /etc/passwd
GREP_COLOR='1;37' grep --color=always 'root' /etc/passwd
GREP_COLOR='1;36' grep --color=always nobody /etc/passwd


In addition, to default red color now we can define colors using GREP_COLOR shell variable. The differnt color helps us massivly with visual grepping.

Saving grep output to a file

Let us say you want to search for keyword acl and save that output a file named my-acl.txt, and then you need to run:
$ grep acl squid.conf > my-acl.txt
We can find tune searching and limit the search to words starting with keyword named acl:
$ grep -w '^acl' squid.conf > my-acl.txt
View the final output using the cat command/more command or less command:
$ cat my-acl.txt

Search for special characters using grep

Want to search [ or ' or ^ special characters using grep? You can ask grep to treat your input as fixed string using -F option or or fgrep command. For example:

## Match '[xyz]' in the filename ##
fgrep '[xyz]' filename #deprecated syntax
grep -F '[xyz]' filename

Use the \ as escape character

To match a character that is special to egrep or grep -E, a a backslash (\) in front of the character. It is usually simpler to use grep -F when you don’t need special pattern matching. For instance, try to match 'foo' by putting \ before ':

grep -E '\'foo\'' input

In sort use the fgrep or grep -F when you do not need to use pattern matching using regex.

Set of metacharacters for a regular expression that need escaping if you wish to match them

See the egrep command for more info and examples about regex:

.
 ^
 $   [
 \   *   \{   \}   \(   \)
.   [   $   [   \   *   \{   \}   \(   \)

The extended regular expression metacharacters are:

.
^
$  [
\  *  +
?  {  }  |  (  )

Suppress error messages

You can tell grep not print out “No such file or directory” or “Permission denied” errors on the screen by passing the -s option. For example:
$ grep -R -i -s 'regex' /etc/
$ grep -R -i -s 'vivek' /etc/

The -s or --no-messages is used to suppress error messages about nonexistent or unreadable files.

Conclusion

The grep command is a very versatile and many new Linux or Unix users find it complicated. Hence, I suggest you read the grep online at GNU.org too. Let us summarize most import options:

grep command in Unix/Linux
Options Description
-i Ignore case distinctions on Linux and Unix
-w Force PATTERN to match only whole words
-v Select non-matching lines
-n Print line number with output lines
-h Suppress the Unix file name prefix on output
-H Print file name with output lines
-r Search directories recursivly on Linux
-R Just like -r but follow all symlinks
-l Print only names of FILEs with selected lines
-c Print only a count of selected lines per FILE
--color Display matched pattern in colors
-m NUMBER Stop grep command NUMBER selected lines
-o Display only matched parts of lines
-B NUMBER Display NUMBER lines of before matched context
-A NUMBER Show NUMBER lines of after matched context
-C NUMBER Display NUMBER lines of output context
-s Tell grep not print out ‘No such file or directory’ or ‘Permission denied’ errors on screen

If you enjoyed the grep tutorial, then you might like to read our “Regular Expressions in Grep” tutorial. Read grep command man page by typing the following man command:
$ man grep
Also, try the help command to learn about grep usage. For example, see grep command in Linux terminal and see what does the ‘-E‘ or ‘-i‘ option does:
$ grep --help | grep -E -w -- '-(E|i)'

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Vivek Gite is an expert IT Consultant with over 25 years of experience, specializing in Linux and open source solutions. He writes about Linux, macOS, Unix, IT, programming, infosec, and open source. Follow his work via RSS feed or email newsletter.

Category List of Unix and Linux commands
AnsibleCheck version Fedora FreeBSD Linux Ubuntu 18.04 Ubuntu macOS
Archivingz commands
Backup ManagementDebian/Ubuntu FreeBSD RHEL
Database ServerBackup MySQL server MariaDB Galera cluster MariaDB TLS/SSL MariaDB replication MySQL Server MySQL remote access
Download managerswget
Driver ManagementLinux Nvidia driver lsmod
Documentationhelp mandb man pinfo
Disk Managementdf duf ncdu pydf
File Managementcat cp less mkdir more tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04 Ubuntu 24.04
KVM VirtualizationCentOS/RHEL 7 CentOS/RHEL 8 Debian 9/10/11 Ubuntu 20.04
Linux Desktop appsChrome Chromium GIMP Skype Spotify VLC 3
LXDBackups CentOS/RHEL Debian 11 Fedora Mount dir Ubuntu 20.04 Ubuntu 22.04
Modern utilitiesbat exa
Network ManagementMonitoring tools Network services RHEL static IP Restart network interface nmcli
Network UtilitiesNetHogs dig host ip nmap ping
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 11 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Power Managementupower
Package Managerapk apt-get apt yum
Processes Managementbg chroot cron disown fg glances gtop iotop jobs killall kill pidof pstree pwdx time vtop
Searchingag egrep grep whereis which
Shell builtinscompgen echo printf
System Managementreboot shutdown
Terminal/sshsshpass tty
Text processingcut rev
Text Editor6 Text editors Save and exit vim
User Environmentexit who
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami w
User Management/etc/group /etc/passwd /etc/shadow chsh
Web ServerApache Let's Encrypt certificate Lighttpd Nginx Security Nginx
WireGuard VPNAlpine Amazon Linux CentOS 8 Debian 10 Firewall Ubuntu 20.04 qrencode
346 comments… add one
  • Ivan Jun 16, 2008 @ 23:22

    I found this tutorial being the most clear and helpful one. Thank you.

    • Sandeep Oct 18, 2010 @ 6:09

      for searching multiple string using GREP command, please use below commands

      grep -ir ‘string1\|string2’

      • Jeyaram May 10, 2011 @ 7:05

        above command is not working, instead

        grep -ir "string1\|string2" dir_name

        • mithun Oct 30, 2013 @ 9:59

          hello sandeep ,
          padhai karke aao and type your answer Here…….padhi nehi karna aata he to gharko ja kar to make cow shed

          • mo Apr 9, 2016 @ 21:06

            Are you serious? And you never made a mistake asking any question? People who put others down to make themselves look superior in intelligence are usually the ones suffering from intellect. People like you follow and can never think for themselves. And people like Jeyaram who possess the balls to ask a questionable question will be the self thinkers. And those self thinkers are the inventers behind what ever you’re memorizing to make yourself LOOK intelligent.

            • aishk Jun 7, 2016 @ 9:17

              Intelligence or our position never said us to disrespect people.i agree with you mo and It is better to say a fool ,that he is a fool.while he was in a imagination that he is the great intelligent in the world.

              • 𝛼Ω Sep 17, 2020 @ 7:33

                whatever you all are thinking, i am the great intelligence in this universe, the only one, in the whole universe, and beyond them. . . cheers and applause!

      • mex Jul 8, 2021 @ 9:24

        I cannot say anything regarding when the comment was made in past, after all it is couple years ago.

        However, both @Sandeep and @Jeyaram .

        only difference is that @Sandeep ‘s code

         grep -ir ‘string1\|string2’

        is finding the matches in the current directory while @Jeyaram

         grep -ir "string1\|string2" dir_name

        specifies which directory he wants to search.

        Find which option is more suits the needs.

    • Eda Sharp Mar 31, 2021 @ 23:22

      I agree. One of the original blog for Linux. Thank you for all hard work.

  • manik sikka Nov 25, 2008 @ 0:26

    This Tutorial is so far helpful to me.

    Thank you..!!

  • turbolinux Dec 12, 2008 @ 11:46

    what if i want to search keyword
    like “$this->Products” or “[‘status’]” ?

    grep -R "['status']" 

    doesnt work

    • arvin Oct 24, 2015 @ 8:16

      grep -R “\[‘status’\]”

  • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Dec 12, 2008 @ 11:53

    Try

    grep -R "\['status'\]"  filename
    • sumit kumar Oct 6, 2017 @ 12:46

      Sir mai ye command chala nhi pa rha hu. Please suggest.

      • Jayant Apr 16, 2021 @ 7:08

        single quote ko bhi escape karo

  • Arunkumar Dec 26, 2008 @ 5:32

    this is very useful to me.. thanks …

  • Vikram Jan 30, 2009 @ 11:39

    Very helpful. saved my time.

  • Domino Mar 18, 2009 @ 21:40

    What is the best way to grep recursively through a directory tree, and display the pattern matches, that occur in just all the *.cpp files?

    For example:

    grep -HRn “look for this” *.cpp

    doesn’t work (on Linux)

    • nRon May 16, 2011 @ 12:32

      You could try

      grep -r –include=”*.cpp” ‘look for this’ .

      (“.” is current directory)

    • durga reddy Aug 12, 2011 @ 13:02

      find . -name “*.cc” |args grep -n “pattern”

      • durga reddy Aug 12, 2011 @ 13:03

        small correction
        find . -name “*.cc” |xargs grep -n “pattern”

  • eswar Mar 23, 2009 @ 9:02

    what if i want to search like this

  • Tauqueer Mar 25, 2009 @ 8:13

    Hi
    I want to search t1.spq, t2.spq ….. tn.spq words from a file
    grep -i “*.spq” filename doesn’t work
    Please tell how can search such words??

    -Regards,
    tauqueer

    • Mohanraj Oct 24, 2011 @ 15:04

      Hi,

      you can try out the below command..

      grep -i ^t..spq filename

      Best regards,
      Mohanraj

  • eswar Mar 25, 2009 @ 9:45

    avoid using * grep -i “spq” tt.sh
    then you will get all the words which will have spq.
    if some thing need to refrained from that need to get the desired out put then use

    grep -i “spq” tt.sh | grep -v ” somepattern”

  • Humphrey Apr 25, 2009 @ 9:03

    How can i grep for an output that come after a statement: eg Expires: 10/May/2009.

    If i want to caputure only the date, how can i grep for what comes after the colon (:)

    please help

    • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Apr 25, 2009 @ 10:21

      Try,

      echo 'Expires: 10/May/2009' | cut -d: -f2
      OR
      echo 'Expires: 10/May/2009' | awk -F':' '{ print $2}'

  • Rizvana Apr 29, 2009 @ 18:01

    This is quite informative…… Thanks.

    Also I have a question, what is the expansion of ‘grep’? Can anyone answer?

    • Shahila Nov 20, 2013 @ 7:39

      globally search a regular expression and print

  • Ravi Apr 30, 2009 @ 7:30

    This helps a lot,,

  • Humphrey May 2, 2009 @ 0:11

    Rizvana,
    grep means Get Regular Expression and Print

  • Humphrey May 2, 2009 @ 20:44

    How can i do calculation on dates;
    eg to know the number of days between ‘todays date’ and a day like ’15/may/2009′

    please help

  • divya May 4, 2009 @ 14:24

    thanks .i gt the rite information.

  • Blury May 11, 2009 @ 3:34

    Hi,
    How can I use grep function to search files that file name with “ord” or “rec” from specific dir??

    • Sandeep Aug 2, 2010 @ 10:19

      grep -ir “ord\|rec” filename

  • kandida Jun 2, 2009 @ 7:04

    Hi
    lets say i have some data :
    a
    a
    a
    b
    b
    b
    c
    c
    c
    can I use grep comand to make like this :
    a
    b
    c
    thanks

    • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Jun 2, 2009 @ 8:04

      Try uniq command.

    • Sandeep Aug 2, 2010 @ 10:20

      grep -ir “a\|b\|c” filename

  • kandida Jun 2, 2009 @ 9:52

    Vivek,
    its working…..thanks a lot

  • Blury Jun 3, 2009 @ 0:51

    Hi,
    No one know how to use grep function to search files that file name with “ord” or “rec” from specific dir??

  • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Jun 3, 2009 @ 1:03

    Try

    cd /dir/to/search
    grep "word" ord*
    grep -R "word" rec*
  • snake Jun 10, 2009 @ 15:46

    Hi,
    I’d like to get the total cpu and memory usage easily and I think of using ‘dstat’ command. Can I get the values corresponding to the free and used column with grep?

    ------memory-usage-----
    used buff cach free
    153M 876k 24M 4392k

    cheers!

  • Nazeem S Jun 22, 2009 @ 10:34

    The Most Helpful POST

  • Thomas K Jun 26, 2009 @ 13:36

    For those who want to search files with wild cards and the like, try the find command with -exec. find /dir/to/search/ -iname *.cpp -exec grep 'word' '{}' \;
    and snake, I do not think it is possible to search columns with grep, I’m 98% sure that it is line (row) only.

  • sankar Jul 10, 2009 @ 6:45

    As i was i beginner , it helped me a lot . I would like to thank all the people who contributed it to the public … than you so much

  • manavi Jul 25, 2009 @ 4:01

    whats the use of egrep and fgrep
    give examples of both

  • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Jul 25, 2009 @ 6:22

    egrep is for regex and fgrep is for fixed string. I will update FAQ with more examples.

  • SANA Aug 2, 2009 @ 4:35

    this is the best and the most understandable tutorial i have seen till date.
    GOOD WORK!!

  • Patrick Aug 7, 2009 @ 10:43

    This tutorial is very easy to follow and enabled me to learn so much within a very short time

  • prathamesh Aug 7, 2009 @ 10:52

    hi this is really really helpful and very fast introduction for grep very nice

  • harshal Aug 8, 2009 @ 10:27

    exact and accurate content with no irrelevent text..

  • Rob Aug 11, 2009 @ 7:19

    clean and accurate .thanks

  • Syed Aug 16, 2009 @ 13:40

    Can someone help me for a data like:

    aa:abc
    bb:def
    cc:ghi
    dd:ijk

    aa:lmn
    bb:opq
    cc:
    dd:uvw

    aa:pqr
    bb:stu
    cc:vwx
    dd:yza

    Description of data:
    aa, bb, cc, dd comprise one record. Blank line is dividing 100 of such records.
    question1) How to grep “cc:” that is empty value? i am unable to do this because it gives all the values.
    question2) I need to print value of “aa:…” for all the records whose “dd:uvw”. How to do this?

  • jyothi Sep 17, 2009 @ 12:35

    hi all..
    can anyone please explain me about this command
    ps -ef|grep -v grep|grep $orasid >>/dev/null

  • learner Sep 22, 2009 @ 23:39

    ok..i feel really dumb..but i got this task i have to do to find a hidden file or something like that and im pretty sure i would use the command ls or whatever.
    my problem is i dont no what im doing at all here..were do i type the command in at? all these things jus teach u the commands. yea im dumb i dont have a clue were to type it in..
    if someone could please help me out here it be much appreciated! thanx

    • Ismael Aug 5, 2011 @ 21:08

      You can get more information in here: linuxcommand.org

    • Basava Sankar Sep 30, 2015 @ 9:55

      It is possible to get grep of some numbers which are there in a list

  • jyothi Sep 23, 2009 @ 12:56

    hii..
    It goes something like this..
    i want to list a oracle database instance (say orasid=tiger) using this command..
    I issued each command in separate and could understand little bit..
    1. ps -ef :: lists all running processes
    2. the output is directed to “grep -v grep” now
    3. what that command does is just filter out any text containing ‘grep’..
    4. this output is now sent to “grep $orasid”
    5. it will just fetch only those running process like ” tiger ”
    6. the output is redirected to /dev/null where the output is just discarded..
    after this command here they used
    if[ $? -ne 0]
    then ————–
    else —————–
    fi
    where $? reads output of previous command…
    here $? is 0 as the command hasn’t thrown any error

  • AKHIL Sep 23, 2009 @ 17:03

    VERY HELPFUL

  • Samer Sep 27, 2009 @ 6:49

    Dear All ,
    Thnaks for this nice chart it helps me a lot .
    1- If i need to search for a “word” inside a directory that holds files, these files are located in the “www”
    2- Also to search for a “word” in the all the databases in : /var/lib/mysql/database

    Thanx all

  • ed Sep 27, 2009 @ 23:48

    What is the grep command to find a string of charactes like below

    KY 41099-1000

    I am just using KY as an example but I want the command to list any state abbreviations followed by a 9 digit zip code. Any help is appreciated

  • nitin Sep 30, 2009 @ 17:06

    @ed
    use
    # grep ^[A-Z][A-Z] [0-9] [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$ filename

    i hope it work………

    • trinadh Mar 28, 2016 @ 19:36

      grep ^[a-z]*[0-9]$ filename

      this is better way to represent.. i hope

  • faiz_zaidy Oct 3, 2009 @ 11:52

    please solve my query
    my query is “how can i search the text in hole file of a particular dirctory”

  • Sam Oct 18, 2009 @ 19:46

    Hi i wud like an explanation of the ff command
    grep[a-g]…[r-z]text.txt

  • Mukesh Oct 25, 2009 @ 1:44

    This Tutorial is very helpful to me.

    Thanks….!!

  • dev Nov 11, 2009 @ 4:26

    @ Syed:

    ** answer1 **
    cat file | grep '^cc:$'
    will produce only cc: with no value.
    Learn regular expressions. In the command above regular expression is between ”. So… cat is normal command that prints file. Then result is piped (|) to grep, which processes it with regular expression (^cc:$).
    This regex basically matches lines, which contain:
    ^ – beginning of the line
    c – letter c
    c – letter c
    : – colon
    $ – end of line
    If there is something between colon (:) and end of line ($), in your case some value, then line is not produced by grep.

    ** answer2 **
    How to check value of aa: in the record which contain dd:uvw?
    cat file | grep -B3 'dd:uvw'
    -B3 tells grep to show line that matches our pattern and 3 lines Before (you can change this number if you want, you can use -A4 to show 4 lines After).
    So the command above will produce whole 4 line of each record, which contain dd:uvw. To show only lines with aa: and their values, we can simply add subsequent grep:
    cat file | grep -B3 'dd:uvw' | grep 'aa'
    …and that’s it.

  • dev Nov 11, 2009 @ 4:37

    @Sam
    grep [a-g]…[r-z]text.txt
    will match files which names start with a,b,c,d,e,f or g, then there are subsequent three characters – each dot (.) represent any character – then there is r,s,t,u,v,w,x,y or z and then text.txt.
    So it can be:
    bokkrtext.txt
    aiiiztext.txt
    and so on…

  • dev Nov 11, 2009 @ 5:14

    @ jyothi
    ps -ef|grep -v grep|grep $orasid » /dev/null

    Explanation:
    ps -ef lists processes. On that list there is number of every process, even number of grep process executed in this line. For example if i try to grep non-existing process john by issuing command ps -ef | grep john, then i will get this:
    dev 6271 5933 0 05:43 pts/0 00:00:00 grep john
    Even if there is no process I am looking for, I will get result from grep. I don’t want that to happen, so I will have to grep invert match (grep -v pattern as explained in the howto above).
    In other words ps -ef | grep -v grep will list processes excluding grep.

    After next pipe you have:
    grep $orasid
    $string recalls value of variable defined before.
    If you define $orasid=bob, then your command will be equal to:
    ps -ef | grep -v grep | grep bob » /dev/null
    If $orasid=william, then your command will be equal to:
    ps -ef | grep -v grep | grep william » /dev/null
    and so on…

    » /dev/null redirects output of the command to nowhere, because we don’t want to see grep output on our screen, when executing the code.

    Summarizing:

    $orasid=tiger
    ...
    ps -ef | grep -v grep | grep $orasid >> /dev/null
    ...
    if [ $? -ne 0]
    then echo 'claws'
    else echo 'no claws'
    fi

    If there is process named tiger (output of grep is not empty), then we get claws, else we get no claws :)

  • marlenchen Nov 16, 2009 @ 19:45

    How can i search users, who can sign in in the system, with the grep commando?

  • swarna Dec 1, 2009 @ 20:12

    very useful to me..

  • parveen dabas Dec 3, 2009 @ 6:38

    i found this tutorial really helpful…
    well i hv one doubt…
    if i have two files, namely..
    cat >file1
    1
    2
    3
    4
    cat >file2
    1
    2
    Now wat command and iptions to use if i want only 3 and 4 as output i.e.
    only those lines which are not present in file2 wen compaed to file1… plz help….

    • Neeta May 14, 2014 @ 0:19

      diff file1 file2

  • krishna Dec 3, 2009 @ 13:17

    Simply super ,,,thanks

  • Shan Dec 14, 2009 @ 17:07

    This is great article , i am using the windows Grep editor, I am search the text content \”*hello*\” to get the world hello in all the files. I didnt get any output . Could suggest me what i should use.

  • parveen dabas Dec 15, 2009 @ 9:44

    Reply to Shan
    wel u can use
    grep -iw hello filename
    if two words ex. Hello and
    Hi, use foll syntax
    egrep -iw ‘hello|Hi filename

  • Ram Jan 12, 2010 @ 21:57

    Please do explain, what do the values between colons represent? For example:

    grep video /etc/groups

    video:x:33:rtalkad

  • hammy Jan 25, 2010 @ 10:21

    How to count total number of lines using shell script..????

  • Runal Jan 28, 2010 @ 10:44

    Hi
    I have an outstanding issue with me…
    i know the specific pattern in the file but dont know the name of the file/script and dont know the location either.
    How to search the filename with simply the pattern in that file.

    Actually at some xyz location a .dmp file gets created (xyz.dmp (everyday it creates the same file as it a daily backup.))…now i dont know which script creates this .dmp file…So i need to know the name of the script.

  • Marco Jan 30, 2010 @ 3:33

    Well, I didn’t found an answer, maybe you can help me.

    I look for something like:

    grep “[string a] AND [string b]” (print all lines who include [string a] and [string b])
    grep “[string a] OR [string b]” (print all lines who include [string a] or [string b])
    grep “NOT [string a]” (print all lines except [string a])

  • Salman Jan 30, 2010 @ 22:59

    @Hammy

    In order to count “words” , “characters” and “lines” in a file there is a bash command called “wc”.

    example:

    wc -l < filename
    or
    cat filename | wc -l
    will count number of lines in file named "filename"

    wc -w < somefile
    cat somefile | wc -w
    will count number of words or strings in the file named "somefile"

    wc -c < somefile
    cat somefile | wc -c
    will count number of characters in the file named "somefile"

    cheers!!

  • esakki Feb 11, 2010 @ 5:50

    How can i found system ip with mac address using grep command, pls tell anyone.

  • my11 Feb 22, 2010 @ 20:19

    how do u run a grep cmd to exclude multiple lines from a file and write remaining lines to a file?

  • naveen.divi Feb 25, 2010 @ 7:58

    this is tutorial is very helpful for me.

    thanks a lot.

  • linu Feb 25, 2010 @ 9:20

    REALLY GREAT BLOG!!1

  • novice Mar 3, 2010 @ 6:29

    hi, I am trying to to something like this…

    filename_$CLIENT_NUMBER_sapphire_$DATE.var
    filename_0_sapphire_20102002.var
    filename_1_sapphire_20102002.var

    filename_22_sapphire_20102002.var
    filename_23_sapphire_20102002.var

    so i wanna get a list of all these vars that are set for a certain day for all the client_number(0 to 24)… in an ordered fashion.

    set | grep 20102002 | grep filename_ — gives me the 0 file and then 10, 11… and then the 1, 2 (filename_0_sapphire_20102002.var)

    i have tried sort and got no results. Please can u help.

    did this too:
    set | grep 20102002 | grep filename_ ‘[0-9]’ — and get only 0 to 9 vars… how do i get 0 to 23 in order.

    Thanks!

  • dillip kumar jena Mar 12, 2010 @ 10:53

    really it is very help ful to me.

  • SNW Mar 16, 2010 @ 21:40

    How you go about listing user and last day of access ?

  • Rutvik Mar 31, 2010 @ 19:34

    Hey,
    I have a question. I have two files and I want to compare the files in such a way that it takes the data that is in file 1 and searches in file 2 and returns me items it did not find. (file 2 has more/newer data). I tried this command but it doesn’t look like it is working.

    grep -v -f Currentsttuf.txt newstuff.txt> difference.txt

  • Ankur Apr 2, 2010 @ 8:51

    Very clearly explained… thanks!

  • vikas kumar May 7, 2010 @ 6:13

    very good ………………
    please send me useful shell programing in my mail..
    vikas kumar

  • Pam Jul 8, 2010 @ 19:11

    my script:

    for f in ‘cat all_logs0708’;
    do
    echo $f
    grep ‘ZYXzyx’ $f >> all_out
    done

    I’d like to have the $f value prefixed on each output line. Is this possible?

  • karthik Jul 9, 2010 @ 12:54

    How to find the files accessed in last 3 days using a grep command.

  • Catherine Jul 12, 2010 @ 23:55

    what if i want to exclude a certain word, for instance ‘prism’
    This what I have so far:
    grep -i ‘ism$’ /usr/dict/words

    i want it to list every word that ends in ‘ism’ but would like to exclude ‘prism’
    i only want to use one grep.

    • Ravi Apr 13, 2015 @ 9:18

      you can grep -v “word which you want to exclude” filename

  • Alima Jul 14, 2010 @ 20:35

    I have 2 files. File1 contains a list of long-strings with their corresponding informations. File2 contains portions of the strings (short-strings) found in file 1. I would like to find which “short-strings” within file2 is present in file1 and extract the corresponding information. The ideal output file should be short-strings + long-string + corresponding information. Please help… Thanks in advance

  • krasaviza Jul 28, 2010 @ 7:42

    Hi xD
    I have a problem with grep -v. I want to exclude a line and the 6 lines before it, so I tried
    |grep -v -B6 ‘my text’ but that doesn’t work, only without the -B6.
    But how else can i exclude the other lines in quite a simple way because i need this option many times :S ?
    Please help me

    Krasaviza

  • Bhavin Vyas Aug 18, 2010 @ 7:50

    I have a data like
    :
    ** SPFS310 LED is on
    LED is on: MB . See prtdiag -v and /var/adm/messages for more details.
    cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=HW;HWTYPE=SYSBOARD
    Mon Aug 9 11:05:50 2010

    * APCL303 Trouble condition asserted.
    Communication with the Core is not established
    cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=APPL;APPLTYPE=SDM_BASE.logs:s
    tart_sdmlaq
    Tue Aug 10 04:38:30 2010

    I want to display the paragraph which contains ** i.e my output should be
    ** SPFS310 LED is on
    LED is on: MB . See prtdiag -v and /var/adm/messages for more details.
    cbm850=TCS-CBM2;NODE=TCS-CBM2-unit0;CLASS=HW;HWTYPE=SYSBOARD
    Mon Aug 9 11:05:50 2010

    What command should I use.Can anyone please tell me.

  • rahul Aug 29, 2010 @ 2:10

    How me difference between egrep and fgrep and how to use these commands

  • sujith Sep 8, 2010 @ 12:58

    Try the below shell programs.

    1. Locate build.number in the input file. Get the value and increment the value to next number for every run.
    2. Have a some parm=value in input file. From shell read these and use it in the shell, pass it to another shell.
    Take two file that have parm=value. From source file copy all the parms that exists in another file (target) with the values retrieved from the source file

  • Cletus Sep 10, 2010 @ 17:32

    I hope this doesn’t sound silly. How do I eliminate a directory from a grep search? Let’s say I want to look for the word “hamburger” that exists somewhere is a file in a subdirectory. I want to recursive search but I want to eliminate one of the subdirectories from being searched. To search I would normally do something like #grep -r hamburger /var/www/* But let’s suppose that /var/www/waterpipe is 2 TB in size and I don’t think hamburger is in there anywhere. How do I search everything else in /var/www/* but not /var/www/waterpipe? MANY thanks.

  • priya Sep 14, 2010 @ 3:07

    plz tel me the command to fetch a line from a file where the 5th col states a particular word like say “YES”

  • Geeta Oct 1, 2010 @ 16:14

    Hi Vivek,
    I have a concerns how to use grep for the following
    1. In a file of 100 lines how to get contents from line number 75 to 90?
    2. Cut lines in a file with index numbers 6, 7, 10, 11
    3. Print lines with index 70 to 95 from a file using head and tail.

    Anticipating u r reply
    Thanks a lot

    • manish Jun 15, 2011 @ 17:50

      3) head -90 filename | tail -25

  • Pradeep Oct 2, 2010 @ 2:56

    grep “^t[0-9].spq” filename

  • Geey Matlola Oct 5, 2010 @ 7:20

    that was so helpful, I now understand how does grep command works. now what soes this rilly specify “ps -ef | grep”

  • Erik Oct 7, 2010 @ 0:57

    Hi to all,
    I just started to learn linux a month ago
    Can I extract 2 to 6 letter words from a text file using one grep command only!
    To mention that each word is on its own line
    what’s the grep command to do this job?
    I tried any combination of grep and not the result which I am looking for

  • Geey Matlola Oct 7, 2010 @ 12:43

    someone reffred me the “man grep” command it rilly helped me to understand it.

  • Tommy K Oct 7, 2010 @ 21:14

    How do I copy a text file but remove any blank lines held within that text using egrep?

  • kuldeep Oct 18, 2010 @ 4:10

    Hi,
    i want to search path for file which has a no say ‘7000000000’ in a server , how i can do this ??
    please help

  • innocentfornow Nov 5, 2010 @ 12:05

    I’ve got to list up all files that located in a folder like /usr/local/somefolder
    to see every file it uses an absolute path for a command like

    require once /some path/some file

    for example
    so I need a wildcard showing all files that uses all commands dealing with
    absolute paths
    for managing a migration to another platform…

  • Ron Nov 9, 2010 @ 12:26

    i need to get the last line from a match with grep although there are multiple matches
    Thanks for the tutorial, i learnt a lot

  • Excellent Apps Nov 25, 2010 @ 9:28

    Great post. Very Helpful.

  • girlatromance Dec 2, 2010 @ 23:50

    Wow. This is exactly what I was waiting for.

  • hoxy Dec 15, 2010 @ 2:42

    I have files as below:

    contents in ‘tempFile’ file
    ===================
    file1.bak.p
    file1.p
    abcd.h
    abcd.bak.h

    how do I search only for the non “.bak: file?

    I tried as below but not working

    grep -v ‘.*\..*\.[a-z]*’ tempFile > tempFile2

    • Harikrishna May 17, 2014 @ 13:04

      grep -v “.bak” tempFile

  • hoxy Dec 15, 2010 @ 2:48

    actually I need to seach for words that only has one occurance of “.”

    as such, ‘abcd.bak.h’ has two “.”

  • vikbenq Jan 5, 2011 @ 23:51

    how can i grep logs using today’s date or say last 24 hours.

    for ex: grep “logs” messages | grep “Jan 5” — i can find logs for Jan 5 using this syntax but I want to use this in a script where I don’t have to use the date every time instead use something like 24 hours

  • Armand Jan 11, 2011 @ 7:05

    I know that it should work like this….

    grep error /home/armand/Documents/errors.txt

    but how do i ignore the ‘shutdown_error’ grep?

  • Gaurav Saxena Jan 25, 2011 @ 8:26

    This is Gaurav..

    Plz tell about:-
    $ grep -l”`echo ‘\t’`” foo

    here foo is a file name….

    thank u…

  • Gaurav Saxena Jan 25, 2011 @ 8:29

    now i can reduce my time in study,by using this website….
    thanks for that…

  • Manu Feb 2, 2011 @ 10:45

    Hey that colour thing is not working on my machine….?

  • Marie Feb 13, 2011 @ 0:23

    How can we grep for patterns in an Arabic file? When I type the pattern to look for, it shows nothing. The file is in Arabic script, but the search on command line does not show that search string?
    Can we read the binary file and search for a particular pattern there? Is there any website/ tutorial on using grep with Arabic script?
    Thanks

  • Dinesh Feb 22, 2011 @ 6:44

    Dear all,
    i have a folder consisting of some hundred txt files.Each file has a description about 10 lines ,then followed by six columns and more than 500 rows.My job( for each txt file) is among the six columns i have to search the string(contains numbers) in the second column ,if there is any matches in any row ,then in the output file i want the few specified lines in the description followed by the row which matches the string.so atlast i want single file which contains the desription of the file as well as the rows that matched the string.Thanks in advance..

  • Vani Feb 23, 2011 @ 8:32

    How to search any string or filname in all the sub-directory starting with same word. Example :
    i need to search the word = ‘dataload’ in all the sub-directory which starts with ‘data2011’ in dir: DATA.
    Where DATA directory has following sub-directory = data2010Jan, data2010mar, data2010july, data2011jan, data2011aug, data2009dec

    • Armand Groenewald Feb 23, 2011 @ 9:17

      you would use

      grep -r dataload /?folders/DATA/data2011*

      Hope that helps :)

    • Armand Groenewald Feb 23, 2011 @ 9:19

      if you want the results to go into a text file
      use

      grep -r dataload /?folders/DATA/data2011* > results.txt

      then you can nano results.txt or vi results.txt or what ever program you want to use…

      :)

  • Pascal Mar 3, 2011 @ 8:32

    I need to know how can i used the line that contain word “accused” without regarding for upper or lower case.using “greb command.
    example: greb “accused” file

    • mikewolf Mar 5, 2011 @ 11:14

      grep -i “accused” file

  • murugesh Mar 13, 2011 @ 4:57

    How do i search a string in two files.

  • N.Srinivasan Mar 13, 2011 @ 16:07

    If some can tell about how to search a particular srting in directory and its sub Directories, It would be of great help!!!!!!!!!!!

  • huisan Mar 18, 2011 @ 7:00

    HI, I wish to ask on how to use grep to produce the output result as below. Thank you very much

    Input
    FEATURES Location/Qualifiers
    source 1..94601
    gene 1..2685

    Output
    FEATURES Location/Qualifiers
    source 1..94601

    • anu Sep 24, 2015 @ 9:34

      it is very hard to understand the grep command
      please easy explanation about grep command

  • type8code0 Mar 20, 2011 @ 6:32

    @murugesh March 13, 2011
    How do i search a string in two files?

    Try

    cat file1 file2 | grep string

  • type8code0 Mar 20, 2011 @ 6:33

    @N.Srinivasan March 13, 2011
    If some can tell about how to search a particular srting in directory and its sub Directories, It would be of great help!!!!!!!!!!!

    Try

    grep -r string /dir/

  • arul Mar 28, 2011 @ 23:49

    Hi,
    I use a grep command to search a file.
    That ouput is sent to a folder.
    Since there are no space in the server, its showing an error like no space in desk

    Any possibility of zipping the output of Grep command before writing to disk?

  • Nayabrasool Apr 18, 2011 @ 15:37

    Pretty good description thank you

  • Heman Apr 21, 2011 @ 4:44

    How do i grep for lines containing a specific string, say “forrest”, while not containing another string “gump”?

  • neeraj Apr 24, 2011 @ 10:22

    There is a variant of grep known as gzgrep to search for a term in the argz archives.

  • Besso Apr 27, 2011 @ 18:35

    Consider the file data.txt which contains the following data (MSISDN, credit, status, error_code, location_id) in the below format
    MSISDN,Credit,Status,location_id,Error_code

    0123318739,13213,A,300,abcde
    0120123456,3423,C,200,xcvfe
    0120453576,5563,A,201,fgsa
    0110445654,3000,A,400,gcaz
    0120432343,3000,A,402,dewa
    0129423324,3000,A,206,dea
    0104323433,3000,A,303,a
    01232132134,3000,A,200,a
    0122344242,4233,N,204,ghfsa
    ————————————————–
    my question is how can i perform the follwing query
    Try to have a command that can match all the below criteria

    • MSISDNs range between 01201xxxxx to 0122xxxxxx
    • And have credit over 3000
    • Status either A or N
    • location_id contains letter ‘a’
    • Error_code starting with 2

    The output of the command you will use when applied on the above file should be

    0120453576,5563,A,201,fgsa
    0122344242,4233,N,204,ghfsa

  • Besso Apr 28, 2011 @ 1:11

    please i need your reply urgently
    ahanks in advance
    =================
    Consider the file data.txt which contains the following data (MSISDN, credit, status, error_code, location_id) in the below format
    MSISDN,Credit,Status,location_id,Error_code

    0123318739,13213,A,300,abcde
    0120123456,3423,C,200,xcvfe
    0120453576,5563,A,201,fgsa
    0110445654,3000,A,400,gcaz
    0120432343,3000,A,402,dewa
    0129423324,3000,A,206,dea
    0104323433,3000,A,303,a
    01232132134,3000,A,200,a
    0122344242,4233,N,204,ghfsa
    ————————————————–
    my question is how can i perform the follwing query
    Try to have a command that can match all the below criteria

    • MSISDNs range between 01201xxxxx to 0122xxxxxx
    • And have credit over 3000
    • Status either A or N
    • location_id contains letter ‘a’
    • Error_code starting with 2

    The output of the command you will use when applied on the above file should be

    0120453576,5563,A,201,fgsa
    0122344242,4233,N,204,ghfsa

    • Dinesh Apr 29, 2011 @ 4:46

      Try this i got correct output.
      $grep -w ‘012[0-2][1-9][0-9][0-9][0-9][0-9][0-9]’ filename |
      grep -w ‘[3-9][0-9][0-9][0-9][0-9]’ |
      grep -v -w ‘3000’ |
      egrep -w ‘A|N’ |
      grep -w ‘2[0-9][0-9]’ |
      grep ‘a’

      • Dinesh Apr 29, 2011 @ 4:53

        important change : change the first line into following two lines :
        $grep -w ‘012[0-2][0-9][0-9][0-9][0-9][0-9][0-9]‘ filename |
        grep -v -w ‘01200[0-9][0-9][0-9][0-9] |

        • Besso Apr 29, 2011 @ 11:18

          Thanks alot you are genuios
          some of the lines worked normally but when I tried to combine them it gives me error so all iam asking is to give me the whole bulk of code to run it in one sentence. Thanks again man you are really a life saver
          BR,
          Besso

          • Dinesh May 5, 2011 @ 4:14

            hi..,i m not getting getting exactly what u r asking ..? they are all piped ones../
            u write all these in .sh file then run…also u want to use these code for single files or multiple files..?

  • swapnil May 4, 2011 @ 7:26

    question : i want to find only the directory in a particular directory.so what is the command in linux to do so.pls help

    • neeraj May 5, 2011 @ 4:06

      @swapnil:
      find -type d

  • Divya May 17, 2011 @ 9:14

    Hi,

    i want to move few file from one location to another. there is no pattern except for one -> “05_ _2011”. these two underscores can be any number. i am not able to move or do a grepfor this pattern.

    Please help.

    • Dinesh May 18, 2011 @ 4:50

      let /home/user/folder be the direcrory contains .txt(or any other extensions) files.Some of these files contains the string ’05_ _2011′.We find those files which contains the string ,and copy files to a new location.
      Save the follwing code as program.sh file
      ———————————————-
      #! /bin/bash
      grep -l ’05[0-9][0-9]2011′ *.txt |
      while read line
      do
      cp /home/user/folder/$line /new/path
      done
      —————————————-
      the files will be copied to a new path with a same file name.
      hope it helps.

  • faizan May 20, 2011 @ 8:11

    its a very helpful for me, thank u
    Regards,
    Faizan Khan
    Pakistan Karachi

  • Gobinda Jun 13, 2011 @ 7:10

    How to grep a log file
    from 07-JUN-2011 01:13:00 to 09-JUN-2011 15:03:04

    my date format is 07-JUN-2011 01:13:00

    • Dinesh Jun 13, 2011 @ 12:33

      $grep ‘0[7-9]\-JUN\-2011’ logfile

      if we specify time, we need to filter more with grep,that makes code difficult and writing lots of lines.Instead get the list for three days, delete the contents before 07-JUN-2011 01:13:00 and after 09-JUN-2011 15:03:04 in a notepad.
      Hope it helps

      • Gobinda Jun 13, 2011 @ 13:03

        its a very helpful for me, thank u
        Regards,
        Gobind

  • matias Jun 29, 2011 @ 20:56

    why are all tutorials incomplete?

    • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Jun 30, 2011 @ 0:43

      What are you talking about?

  • shihab Jun 30, 2011 @ 3:48

    Hi,

    This helps a lot, Could you please explain grep command.

    ” ls -l | grep -a “^d” | tee dir.lst | wc -l “

    • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Jun 30, 2011 @ 5:39

      The grep will get only name of directories from the ls -l output.

  • Ihab Jul 8, 2011 @ 15:01

    How can I grep for 2 strings (string1 and string 2) in 2 log files: log1 and log2 in one command? I also need to show 3 lines before and 3 lines after each string.
    Thanks in advance.

    • Dinesh Jul 11, 2011 @ 4:16

      for the first step you use the following command
      $ cat log1 log2 | grep ‘string1.*string2′
      I didn’t get the appropriate meaning for ’3 lines before and 3 lines after each string’
      hope it helps

      • sid Apr 20, 2012 @ 16:42

        @lhab
        Can you try this and let me know if it suits your requirement:

        $ cat log1 log2 | egrep ‘string1|string2′| egrep -A3 -B3 ‘string1|string2’

        • jetole Apr 21, 2012 @ 7:15

          This is what the bash zealots refer to as unnecessary use of cat.

          either
          grep -e ‘string1’ -e ‘string2’ log1 log2

          or
          grep ‘string[12]’ log1 log2

          or
          egrep ‘string(1|2)’ log1 log2

          though… assuming string1 and string2 are completely different / not entirely the same except for the last two into digits then we could use either
          egrep ‘(string1|string2)’ log1 log2

          or
          grep -E ‘(string1|string2)’ log1 log2

          or
          grep -e ‘string1’ -e ‘string2’ log1 log2 (which I already mentioned)

          Enjoy! :-)

          • jetole Apr 21, 2012 @ 7:16

            I meant to say “int digits” referring to integers and not “into digits”.

            …long night.

  • Dinesh Jul 11, 2011 @ 4:16

    for the first step you use the following command
    $ cat log1 log2 | grep ‘string1.*string2’
    I didn’t get the appropriate meaning for ‘3 lines before and 3 lines after each string’
    hope it helps

  • Ihab Jul 11, 2011 @ 6:22

    Dinesh,
    Thanks for your reply. What I am trying to search for is a log with multiple daily entries. One log may contain many days worth of entries. One day of log entry can be located in 2 different logs (log1 and log2). For example:

    1) Let’s say that I want to search all the log entries of July 1, 2011 that are located in log1 and log2.
    The first string in the log entry is the date which has the following format: 2011-07-01 and the second string which can be anywhere in the same log entry: ‘transaction failed’ or ‘error’ or ‘unsuccessful’.
    So string1 = 2011-07-01 and string2 = ‘transaction failed’ or ‘error’ or ‘unsuccessful’.
    2) Each log entry can be up to 6 lines long. Log entries are seperated by 1 blank line and another line says: END OF ENTRY. I believe grep command would only display the line that contains both strings (string1 and string2) but I would like also to display the whole log entry that contains both string1 and string2 if possible.

    Regards,
    Ihab

  • ROHIT Jul 11, 2011 @ 11:59

    I have to search for two words..for ex animal …after it is present i need to search for dog …Basically i have to AND both the terms…suggest me the command?

    • anoop Jul 26, 2011 @ 12:27

      hi rohit
      u can use pipelining command
      for example
      suppose ur file name is animal_list
      then u give a command like this
      cat animal_list | egrep -i ‘ex | animal’ | grep -i ‘dog’

  • Gobinda Jul 15, 2011 @ 12:57

    In RHEL 4 update 8 RAM is 16GB what will be the suggested SWAP and kernel parameters for running oracle 10.2.0.3

  • anoop Jul 26, 2011 @ 12:23

    very intresting and good way.. thanks

  • naveen Jul 27, 2011 @ 11:06

    this is useful everyone

  • kintelk Jul 29, 2011 @ 8:16

    hi

    this site is very hopeful to me !
    thanks to this website , i can lean the grep command esilry~
    bye. i will expect more precious info .

  • rajasekhar vinjamuri Aug 2, 2011 @ 7:48

    how to find a string in a file..that string is middle of the file

    grep ‘^string’ file name..starting of the line
    grep ‘string$’ filename ending of the line

    then how to fine that string middle of the line by using grep

  • i Aug 11, 2011 @ 13:26

    can somebody give me examples of

    grep –color=auto foo myfile
    $ grep –color vivek /etc/passwd

    I need to find a string using grep then i want to see the searched text in a specified color where it’s present.

  • Jesse Leal dos Santos Aug 18, 2011 @ 21:14

    Nice tuts. Thanks!

  • jsengu Aug 25, 2011 @ 6:09

    Hi…. I have a xml file named ” Customer_REPEAT_159.xml”, this file has an element named
    940547722

    My requirement is to use a shell script to extract the value of the ARC_ACCT_NUM; i.e. 940547722, and rename this file, to (ARC_ACCT_NUM)_CurrentTimestamp, i.e. 940547722_20110825114523.

    Can someone help me out in this?
    Please let me know, if any further information is required on this…. :)

  • amirali Aug 25, 2011 @ 8:40

    it was realllyyyyyyyy awsoome! really useful! thank you very much!

  • Ram Sep 6, 2011 @ 5:18

    This tutorial s realy gud..simple and lso 2 point..

  • Brian Sep 8, 2011 @ 5:17

    I have a line in a file i.e.

    /a/b/c/d

    How do I use grep and awk to print
    a b ?

    Thanks

    Brian

    • Brian Sep 10, 2011 @ 8:42

      cat file | awk -F ‘/’ ‘{print$1, $2}’

  • santhosh Sep 9, 2011 @ 4:45

    Wonderful,simple ,nice and clean Thankyou

  • sudheer Sep 11, 2011 @ 2:55

    1) Use grep (or awk) to output all lines in a given file which contain employee ID numbers. Assume that each employee ID number consists of 1-4 digits followed by two letters: the first 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:

    a. A file with the regular expression which can directly be used by grep (or awk)

    b. A text file 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 different 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 file 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 identifiers 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.)

  • sapna Sep 22, 2011 @ 15:09

    Q1)by using grep command how do display all those lines in a file that has only uppercase characters?
    Q2)by using grep select all those lines which has a ?(mark) at the end of line?
    Q3) by using grep command how select all those lines which has any of the following patterns ab,aab,aaab or like others?

  • Aim&fire Sep 29, 2011 @ 3:42

    How can I use grep with wc and uniq commands? I know I have to use piping, but I can’t seem to get the order right. Can someone help?

  • ghafil Oct 18, 2011 @ 10:18

    how to do grep -v ‘pattern1|pattern2|pattern3’ with invert search?

  • chandu Oct 19, 2011 @ 12:03

    very usefull………

  • David Nov 3, 2011 @ 18:46

    Can anybody give me an idea what these grep commands are doing? I know they are specific to the business process where I am, but any insight would be helpful.
    $tzfile is a timezone file
    $alist is a file with three lines each with a numeric value

    grep NEWPOS $tzfile |cut -c1-4 >$alist
    grep FUTQP $tzfile |cut -c1-4 >>$alist
    grep CLB $tzfile |cut -c1-4 >>$alist

  • zip Nov 21, 2011 @ 15:23

    how do i do this- grep -w “…” filename
    with the comand “sed”?

  • siva Nov 23, 2011 @ 8:04

    Hi ,

    I’m new to linux , I would like to know how to identify the blank files in linux for example
    [root@xxxxxx]# cat dfpAL52sS5028817

    [root@xxxxxx]#
    [root@xxxxxx]#
    [root@xxxxxx]#

    I would like to identify the files that shows blank as above , can any one help me this.

    advance thanks,
    Siva.

  • jetole Dec 2, 2011 @ 13:47

    while some of this may work, it’s a rather inefficient way to do it. You can read the man page for both ps and grep by running the commands “man ps” and “man grep” (without the quotes) to get a good idea of what all the options are. In doing so you can see that you can specify the command you are looking for in ps using the -C option, you can format the output to look how you want with the -o option and you can remove the headers which usually become negligible when using the -o option by adding the –no-headers option. Additionally with grep their is a -q option which causes grep to be quiet so there would be no need to redirect output to /dev/null and another useful option would be -w which matches word only. Additionally, if you run the command “help if” (without the quotes) or “man bash” and skip to the if section, you will see that the if clause evaluates the return of a command. “[” in a if clause is a command (either a shell built-in usually or can be an external command. You likely have both but the shell built-in takes precedence. Because of this, you can use grep as the command if looks at instead of the [ command/clause and narrow down your code a lot more to look something like this:

    orasid="tiger"
    if ps -C oracle --no-headers -o cmd | grep -qw "${orasid}"
    then
            echo "${orasid} found. PID's of oracle containing ${orasid} listed below"
            ps -C oracle -o pid,cmd | grep -qw "${orasid}"
    else
            echo "No instance of oracle found with ${orasid} in the options"
    fi
    

    I’m using ${variable} here instead of $variable which isn’t supported under all shells but it is supported under bash. It’s not necessarily needed here however I prefer to use most of the time (there are some instances where you shouldn’t use it) because I consider it a safe way to protect against errors where perhaps part of the text is not part of the variable, for example, if my variable named num containing the number 5 and I want to echo “The 5th number” where I use the variable num for 5 then, if I wrote

    num=5
    echo "The $numth number"
    # This would print "The number". It would not know that th isn't part of the variable and it would
    # think the variable is all of $numth which doesn't exist so it wouldn't print anything for that var
    

    instead

    num=5
    echo "The ${num}th number"
    # This would print "The 5th number". Using ${} we define exactly where the variable starts and
    # ends so it knows that num is the variable and the "th" is just text to echo.
    

    Again, the ${} isn’t portable and may not work outside of bash but, on the other hand, bash has been around longer then Linux (bash since 89, Linux since 91) and is the default shell on most Linux distros so it’s there to use and really worth it.

    Another plus about using bash, again, this isn’t portable and may not work outside of bash but you can use if [[ my_test ]] instead of if [ my_test]. [[ should only be a shell built in. What I mean by this is that [ can either be a shell built in function (and most commonly it’s the shell built in you use or [ can also be a command. [[ is almost never the name of a command and hence why it’s not portable but if you are using bash and you probably should be unless you know why you want to use another shell more but if you are using bash then [[ and ]] is less error prone then [ and ] and allows you to perform multiple checks within the same test clause, for example if you wanted to test to make sure one check is correct or another check is correct using [ and ], the safest way to do so would be to have to separate if statements outside of each other both performing the same action i.e.

    if [ 1 == 1 ]
    then
            perform this action
    fi
    
    if [ 2 == 2 ]
    then
            perform that exact same action
    fi
    

    In theory you can use:

    if [ 1 == 1 ] || [ 2 == 2 ]
    

    However chaining or statements like that in multiple commands to the if statement to see if either one is true is frowned upon and is said that it may cause unexpected results. The proper way to do this from bash would be:

    if [[ 1 == 1 || 2 == 2 ]]
    then
            perform this action
    fi
    

    If you try to use:

    if [ 1 == 1 || 2 == 2 ]
    then
            perform this action
    fi
    

    then it will fail because it will treat the || inside the [ and ] as a bash || statement between two command thinking “2 == 2 ]” is a second command and will complain that you are missing the closing ] on the first command. Using [[ and ]] you can also use an AND clause to all tests are true instead of if any one test is true, for example

    if [[ 1 == 2 || 2 == 2 ]]
    # This returns true and performs the action in the if clause because it only needs one of the two to be true.
    
    if [[ 1 == 2 && 2 == 2 ]]
    # This returns false and performs the action in the else clause of nothing if their is no else clause and this would require both statements to be true which they are not because 1 does not equal 2.
    

    For more details on all the operators you can use within [ ] and [[ ]], run the commands “help test”, “help [” or “man bash” (without the quotes) and if you look at the man page for bash, jump down to the section “CONDITIONAL EXPRESSIONS” (again, without the quotes).

    Another handy test method to use inside bash is (( and )) for number only comparison. You can use [[ and ]] to test if a file exists or if a string is the string you want etc but (( and )) are only for numbers (more specifically integers but I’ll dive into that in a moment). which can help make sure you are not accidentally testing something you shouldn’t be. Also you can use “declare -i” (sans the quotes) to define a variable that can only hold an integer and if anything is assigned to it that is not an integer then it will default to 0.

    declare -i my_variable="boat"
    echo "${my_variable}"
    # This will print 0 because my_variable doesn't allow strings when defined with declare -i
    
    declare -i my_variable=15
    echo "${my_variable}"
    # This will print 15 because it will only accept integers.
    
    declare -i my_variable=15
    if ((${my_variable} > 10))
    then
            echo "${my_variable} is greater then 10"
    else
            echo "${my_variable} is 10 or less then 10"
    fi
    # This will print "15 is greater then 10". If you change the var to 10 or lower then it will execute the else statement.
    
    declare -i myvar=1.5
    # This will cause an error because integer variable only allow whole numbers.
    
    if (( 1.5 == 1.5 ))
    # This will also cause an error because (( and )) will work with integers / whole numbers.
    

    You can store decimal numbers in regular variables though there is no special var designed only for decimals. There are a lot of different ways to test decimal numbers but the best, IMHO, is using the command bc command. You can use this inside a (( )) test by calling the bc command using command substitution with $(command). People often use back ticks, ” `command` ” (sans the double quotes), for command substitution which, again, back ticks are portable and $( ) doesn’t work in all shells however, if you are using bash and you probably should be using bash then $(command) is more more powerful and back ticks are much more prone to error. When using bc to perform a comparison, for example, if x < y, then bc prints 1 when it is true (if x really is < y) else it prints 0 if the comparison is false (compare x < y but if x is greater then or equal to y then x < y is false so it prints a one). Here's an example of how to compare decimal numbers.

    myvar=12.5
    if (( $(bc <<< "${myvar} < 15.2") == 1)
    then
            echo "${myvar} is less then 15.2"
    else
            echo "${myvar is greater then or equal to 15.2"
    fi
    
    # The if statement is checking to see if the number printed by the $(command) is equal to 1.
    # bc is saying is 12.5 less then 15.2 which it is so the comparison is true so bc prints 1
    # Knowing that bc is printing one, then the if statement could be simplified for demonstation purposes by saying
    
    if (( 1 == 1 ))
    # That doesn't make sense to test just because it's too simple but that gives you an example of how the if statement looks to bash based on the result provided by bc in the command substitution.
    
    # If you just want to do basic math on decimal numbers in bash you can do something like
    
    num1=12.5
    num2=15.2
    sum=$(bc <<< "${num1} + ${num2}"
    echo "The sum of ${num1} + ${num2} = ${sum}"
    # This will print: The sum of 12.5 + 15.2 = 27.7
    

    I think I have gone pretty sufficiently above, beyond and way off topic here so I am going to quit before I bore anyone to death but I really did want to enlighten people to the power of bash and it’s capabilities and I believe I have done a good job at starting that so far so I’m gonna quit while I’m ahead, if I am ;-)

  • jetole Dec 2, 2011 @ 13:53

    Oh P.S.

    I don’t know what the name of the oracle server is. In my first example I used:

    ps -C oracle
    

    This was assuming the name of the command running is oracle however if you know part of the command name then you can find the exact command name by running:

    ps -A -o ucmd --no-headers | grep -i oracle
    

    The output ucmd shows only the command running without any arguments unlike “-o cmd” which shows the command and all arguments so when you use “-o ucmd” and do a case insensitive search for oracle, it will return any commands that are either named oracle or have oracle as part of their name but it will not return the grep command because ucmd isn’t showing arguments so the ps listing, for the line which matches grep will just show “grep” and not “grep -i oracle”. When you run the above command to find your commands name, you can replace oracle with all or part of the command you are looking for and it will return any patches, case insensitive (-i option for case insensitive).

  • Manish Dec 7, 2011 @ 5:34

    so much helpful nd clear document………………………………………

    thanks

  • ayat sbih Dec 11, 2011 @ 20:54

    can you help me to learn how i can replace one word with another?

  • Shehzad-Bilal Dec 21, 2011 @ 7:22

    Great tutorial

  • mj Dec 30, 2011 @ 6:13

    i need find the starting “p” in all the files at a single directory.

    • Babu Aug 1, 2012 @ 11:27

      grep ‘p*’ /.

      • Sangita Aug 9, 2012 @ 11:15

        grep -i ‘p*’ /

  • vivek Jan 2, 2012 @ 14:49

    i need to grep only one occurrence of character / in std output another unix coomand on LINUX platform as example below :-

    jar -tf *.zip | grep ‘\/’
    installer/
    installer/lib/
    installer/lib/
    ………

    anyone please help me with solution

  • Akiva Jan 5, 2012 @ 7:51

    thanks

  • perg Jan 20, 2012 @ 5:51

    Thanks, was helpful even 4 years later!

  • simmy Jan 22, 2012 @ 15:59

    really super site to learn linux grep command

  • joanna Jan 23, 2012 @ 7:00

    what if we want the output of one command, minus the output of another command… ? by using grep -v?
    for example ls and ls file.txt (i.e print the output of ls, except the ones related to file, using grep… )

  • jay Feb 6, 2012 @ 3:38

    how to find/search particular date & time when we have more than 1000(times and date) stored in a log file?

  • dv5855 Feb 21, 2012 @ 22:28

    Your explanation is exactly what I needed.

  • Alex Feb 26, 2012 @ 4:13

    How would I find the number of words in a file which do not contain any of the letters a, e, i, o, or u?

    • Alex Feb 26, 2012 @ 4:33

      wait never mind i figured it out — the tutorial was very helpful in letting me understand grep.

  • Shreya Mar 8, 2012 @ 16:45

    Hi,I get multiple entries as given below in logs, If i find the keyword ” Failed to process change of products” in logs, then i want to print the partyid (which is at 4th line given below) i.e (13312221435179143122818) number should be printed.
    Can some one please help me ?

    Logs:
    Failed to process change of products . Message XML is [

    13312221435179143122818

    13744
    CN
    2012-03-08T16:24:38Z

    • jetole Aug 23, 2012 @ 21:59

      grep -A3 “^Failed to process change of products” data | grep ‘^[[:blank:]]\+[0-9]\{10,100\}[[:blank:]]*$’ | sed -e ‘s/^[[:blank:]]\+//;s/[[:blank:]]\+$//’

      This is actually three commands where we pipe the output of one command as the input to another. I created a test file called data. The first command is

      grep -A3 “^Failed to process change of products” data

      This is saying to search the file named data for a line that starts as “Failed to process change of products”. The ^ at the front represents the start of the line. The -A3 option says to return 3 lines after the match. Your match is the first line and then the three below it make 4. The next command

      grep ‘^[[:blank:]]\+[0-9]\{10,100\}[[:blank:]]*$’

      This one gets a little trickier. We have a match to a whole line (which be careful when you write those). The $ symbol represents the end of the line so all characters always have to be between ^ and $. the part after the start of line, [[:blank:]] represents any blank character. This can be a tab or a space or there are a couple other matches (man 7 regex). The \+ says to match as many of these as you see referring to the item right before it. This means if it sees one space or 20 spaces it all becomes part of that regex. If you search for “a[[:blank:]]b” then you are searching for exactly “a b” with one space or one tab, etc. If you are searching for “a[[:blank:]]\+b” then you could be searching for “a b” or “a b” or any variation of spacing in that order. [0-9] says to match all number characters (between 0 and 9) and the \{10,100\} says how many have to match so by saying 10, 100 it means we need to find a string of numbers with between 10 digits and 100 digits. This makes sure if get a short number that turns out to be not really the number we wanted and if we know the number is going to be at least x digits then we can use logic to eliminate false positives. This is followed by another [[:blank:]] and now with a * witch means match non or more. Unlike \+ witch matched one or more, * is ok if it doesn’t see any. This is followed by the $ for the end of line. What this rule means is match any line with an unknown amount of spacing at the start followed by a number at least 10 digits long and no longer then 100 digits long. We then say if there is any space at the end of the line to match that as well and then, either way, that has to be the end of the line. This means if the line contains anything other then space to numbers and maybe space then we don’t want it. if we insert a letter or symbol anywhere on the line then it wont match. If there is no spacing at the start of the line then it wont match. The last command,

      sed -e ‘s/^[[:blank:]]\+//;s/[[:blank:]]\+$//’

      This is actually two sub commands for sed combined. The -e option says execute this sed request. The ; symbol ends the command and you can start another one (this applies to bash and most shells and many programming languages too. The two commands are

      s/^[[:blank:]]\+//
      s/[[:blank:]]\+$//

      The first one says if there is any empty space at the start of the line then to delete it. The format is s/search/replace/ so if I wanted to replace the word cat with dog I would use s/cat/dog/ where it matches cat and replaces it with dog. Using the two forward slashes at the end says replace it with nothing or delete it. The second rule says if there is any space at the end then delete it. This gives you back just the number without any tab/space padding on either end.

      I just realized how I could have wrote the second grep and the final sed as just one sed but I’ve said too much already.

  • raj Apr 1, 2012 @ 19:17

    hey
    can u help me with this i’m trying to find all the file extension present in a directory

    like i have a folder with files
    rqed.txt
    few.pdf
    fwd.xml
    fwesdx.c
    gfwd.sh
    frw.mp3
    fws.avi
    then grep command should get all the extension txt pdf xmlc sh mp3 avi..

  • jetole Apr 2, 2012 @ 3:28

    Not sure this is a grep “thing”. If you just want to find all files with a certain extension in a directory then run
    find . -type f -maxdepth 1 -iname ‘*.pdf’

    If you want to see all file extensions in a dir then run
    find . -type f -maxdepth 1 -iname ‘*.*’ | sed -e ‘s/^.*\.// | sort -u

    • raj Apr 2, 2012 @ 15:17

      getting an error with this syntax
      sed: -e expression #1, char 1: unknown command: `�’
      find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

  • Tom Apr 23, 2012 @ 20:11

    Hey guys,

    Do you guys know how to use the grep command when in a txt.file or any file.
    for example when there are so many lines, how do you the grep command to return the result and its highlighted?

    /someword — highlights the result

    • 👮🛡️ Vivek Gite (Author and Admin) nixCraft Apr 23, 2012 @ 20:25

      Tested with gnu/grep :)

      grep --color 'word' file
      grep -o --color 'word' file
      
      • Tom Apr 23, 2012 @ 21:27

        Can you explain what GNU/Grep is?

        Here is what im trying to do. When I vi or view (ie View sometextfile) I want to be able to grep or / and find the word?

        Thanks,
        Tom

        • jetole Apr 24, 2012 @ 1:42

          vim is a command (and view is a symlink to vim). grep is a command. While vim gives you the ability to call commands from inside of it, it’s not what you are referring to or what you want. If you want to search from within vim (which has nothing to do with grep and cannot have anything to do with grep, then you press / followed by your search term, for example if I wanted to search for “dogs” inside a document I am accessing via vim/view then I would press /dogs followed by enter though it will start trying to match immediately before I hit enter but will go back to where it was initially if I press escape.

          If you want to highlight search matches in vim/view then type :set hlsearch followed by enter and all of your matches will now be highlighted. If you want to turn the current highlighting off without searching for something else (even non-existant) then type :nohl followed by enter. If you want to make sure that vim does this highlighting automatically then add the following line to your ~/.vimrc: set hls and that will turn hlsearch on by default (which I personally prefer to have this set in my .vimrc).

          Seeing as how vim/view have nothing to do with grep, please don’t reply to this comment. It’s better for you to start a new thread or follow up on a existing thread relative to the topic instead of asking about new topics in existing threads about other topics but seeing as how I can tell you didn’t know the difference before you asked, that’s why I chose to give you an answer anyways ;-)

          Hope this helps.

          • jetole Apr 24, 2012 @ 1:45

            P.S.: My reply was supposed to have a more clear representation of the commands but I should have read allowed HTML commands first. Oops.

            For clarity, these are the lines/commands I meant to have stand out:
            /dogs
            :set hlsearch
            :nohl
            set hls

  • Paul May 3, 2012 @ 15:29

    What Im wondering is where the “HERE” in below is a variable or other..
    It works, but Im not sure why its needed?

    In this find, Im looking for files in the directory that are 10days or older and then gzipping them. Its more complex than just this, but this was the previous find before we fixed a production problem that is now ok.. This runs ok btw.

    Im new enought to this to not be sure whats what.

    find $log_directory -type f -mtime +10 | xargs -iHERE gzip HERE

  • Paul May 3, 2012 @ 15:40

    Another grep question:

    Lets say I want to look at files in a directory that has all sorts of dates for their files.
    SALES might be SALESTulsa.txt or SALESDallas.txt but differnt dates.

    Scenario:
    ls | grep SALES | grep ‘May 3’ – would give me the files for that date.

    But how would i get may 3 & may 2? or even just current date (today)?

  • Paul May 3, 2012 @ 16:02

    in the above… some sort of ‘may 2’ or ‘may 3’ mix would be what Im after.

  • jetole May 4, 2012 @ 3:20

    Paul,

    On my system and I think this is standard on all *nix, not sure, but on my system, ls -l shows dates like 2012-05-03.

    With dates like such, there are a few ways to grep this out:
    ls -l SALES* | grep -w ‘2012-05-0[23]’
    ls -l SALES* | grep -w -E ‘2012-05-0(2|3)’
    ls -l SALES* | egrep -w ‘2012-05-0(2|3)’

    [ and ] is used to match any of the chars between [ and ] so [23] matches either 2 or 3. [0-9] matches any single number. [a-f0-9x_] matches a through f, 0 through 9, x or _. The -w option says match that word so it will match 2012-05-02 and 2012-05-03 but not 2012-05-026 or x2012-05-03. The -E option is extended regex (though you really don’t need it in this case) and egrep is grep set to automatically do the extended regex without the option. The first example really should be all you need but extended grep can be used for more complex matches for example:

    egrep -i ‘(dog|bird) house’ pet_accessories.txt

    That would go through the file pet_accessories.txt and match any lines containing “dog house” or “bird house” regardless of upper case or lower case (-i option).

  • BIGuy Jun 5, 2012 @ 10:30

    Thanks Vivek, great tutorial!

  • Nemanja Jun 7, 2012 @ 9:12

    hi everybody,
    how i can search String in whole Folder and subfolders?

  • jetole Jun 7, 2012 @ 16:33

    Nemanja,

    -r

  • Raj Jun 14, 2012 @ 23:34

    I want to just redirect o/p to different file who is between “2012-06-14 13:00:00” – “2012-06-14 14:00:00”.

    I have tried comparing the $3 with epoch time but if file is huge it takes around 15 mins to parse every single line time. Could you please help me with efficient command that will fetch data with above mentioned file and redirect it to New.dat?

    file Name Raj.dat
    CLASS 2012-06-14 11:04:41,076 3 A 22
    CLASS 2012-06-14 11:11:56,217 4 B 23
    CLASS 2012-06-14 11:31:41,601 5 D 23
    CLASS 2012-06-14 12:34:12,813 1 B 45
    CLASS 2012-06-14 12:35:57,845 2 H 12
    CLASS 2012-06-14 12:49:43,112 3 O 44
    CLASS 2012-06-14 12:04:41,076 3 A 22
    CLASS 2012-06-14 13:04:41,076 3 A 22
    CLASS 2012-06-14 13:11:56,217 4 B 23
    CLASS 2012-06-14 13:31:41,601 5 D 23
    CLASS 2012-06-14 14:34:12,813 1 B 45
    CLASS 2012-06-14 14:35:57,845 2 H 12
    CLASS 2012-06-14 14:49:43,112 3 O 44
    CLASS 2012-06-14 15:04:41,076 3 A 22
    CLASS 2012-06-14 15:11:56,217 4 B 23
    CLASS 2012-06-14 15:31:41,601 5 D 23
    CLASS 2012-06-14 15:34:12,813 1 B 45
    CLASS 2012-06-14 15:35:57,845 2 H 12
    CLASS 2012-06-14 15:49:43,112 3 O 44

    ———–
    Thanks,
    Raj

  • jetole Jun 17, 2012 @ 17:14

    grep -w -e ‘2012-06-14 13:[0-9]\{2\}:[0-9]\{2\}’ -e ‘2012-06-14 14:00:00’

  • jetole Jun 17, 2012 @ 17:17

    I’m thinking I can’t guarantee what I just would be the fastest option out there but this is a grep forum board we’re on and that’s one of many ways to do it in grep. If I really wanted the fastest time then I would import it into mysql and use RDBMS queries to select where timestamp >= x and timestamp <= y.

  • Jason Jun 28, 2012 @ 15:40

    In your 6th example, “Use grep to search 2 different words”, you use the command:
    $ egrep -w ‘word1|word2’ /path/to/file

    Is the “egrep” intentional? Or is it a typo.

    Jason

  • zhaoyou Jul 4, 2012 @ 9:08

    cool, it is good for newer

  • Sushant Singh Jul 20, 2012 @ 6:45

    does the command ‘grep cron messages’ show all the scheduled tasks?

  • jetole Jul 20, 2012 @ 11:20

    No. grep shows matches of a word in a file. You can use at to list scheduled tasks through at. You also need to check out the crontab files/dirs in etc and also each users crontab personal file which I believe would be /car/spool/cron (not sitting in front of computer) or can also be viewed by running: sudo -u crontab -e

  • jetole Jul 20, 2012 @ 11:22

    I meant to say you can use atq but my phone auto corrected it.

  • sinah Aug 23, 2012 @ 16:14

    Hi everyone!
    I want to write a command that will search all files in the current directory for any files that contain the word “cat”.By using a grep command

    • jetole Aug 23, 2012 @ 21:26

      did you the command
      man grep
      If that doesn’t work try
      grep -riIl ‘cat’ .
      -r is recursive meaning it searches recursively in whatever directory you specify. -i is case insensitive so it matches cat, CAT, cAT and more. -I says ignore binary files and don’t check them. This speeds the search up and it means it’s not looking in the content of a executable or any other type of file not designed to be read by humans (this would include a write doc as excluded so leave that option off if you need a match in those types of files but keep in mind words are not necessarily stored in the format you wrote in those types of docs so it might not match what you’re looking for either if that’s the type of file you’re looking to match in. -l says list the files. This shows you all files that match but doesn’t show you each the matching line. If you don’t use this option it will display every line in the file that matched your regex. Speaking of regex, try the command
      man 7 regex.
      It’s very handy and useful :-D

      • jetole Aug 23, 2012 @ 21:27

        Opps. Those commands were supposed to be in a code box. The last command is

        man 7 regex

        Not “man 7 regex.” (no period).

  • Michael Sep 1, 2012 @ 7:10

    Thanks a lot. Straight and to the point.

  • NILESH Sep 29, 2012 @ 6:15

    what is a ‘grep’ command to find the users who are not using terminals pts/2, pts/3.

  • Shahzad Hussain Oct 10, 2012 @ 12:29

    can you help me how can i learn Unix and shell scripting within few months (3 months)

    waiting for reply on mail

  • pupu Oct 22, 2012 @ 12:53

    cat /etc/passwd | grep -i -e ‘Administrator’ > output.txt

  • robert peregrin Oct 23, 2012 @ 13:03

    Hi,

    I’m working on filtering lines within a CSV file which would have one column with no data. I need help on how to do it.

    Here is a sample of the line in the CSV file:

    CDR,78,2,0:0,0,5084D8203B8902160D0F01062C02,,,,,,,,8519,6234,154,,,,,,22 Oct 2012 08:22:40.999,,22 Oct 2012 08:22:46.002,,22 Oct 2012 08:22:48.029,22 Oct 2012 08:22:48.
    347,,,4,1,966502536625,4,1,2348056055035,,,,,,,,,,,,,,,,,,,,,,,32:1,18:36,10,128:144:163,16,132:144,,,1,,,,,,,,,,,,,,,,0,,,,0,3,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,22 Oct 2012 08:22:40.999,22 Oct 2012 08:22:48.029,0,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    ,,,,,,,,,,,,,,,,,,,,END
    

    I need to search the whole file for all lines with field number 32 as blank. for example:

    4,1,,4,1,2348056055035

    Please help advise on how I can do it with grep or any other filter.

    Regards,
    Robert

  • Jan Jan 9, 2013 @ 2:00

    Hi,
    great article, just one smartass-necessity:
    cat file.txt | grep ‘some text’
    and
    grep ‘some text’ file.txt
    do actually the same, with the difference that the first line uses an additional process, ‘cat’. So the second line is more efficient and less to type :)

    Greets,
    Jan

  • Rao Jan 11, 2013 @ 10:20

    Need a small help w.r.t to grep.. I have file which contains some strings with “.wav” word. When I search using grep I am getting a lines which contains the keyword i am searching for.
    Now the problem is I want to fetch the word that is coming along with .wav also , for example
    File content:
    =============
    “2013-01-10/10:08:45.049 METRIC 0002010E-2498E59A prompt /usr/local/phoneweb/tmp/0002010E-2498E59A-0001//promptsfile.0002010E-2498E59A-0001.3:audio|file:///opt/product/tomcat/webap
    ps/LycaIVRS_2.1.8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/validityexpiry.wav;audio|file:///opt/product/tomcat/webapps/LycaIVRS_2.1.8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/Digits
    /mon-01.wav;audio|file:///opt/product/tomcat/webapps/LycaIVRS_2.1.8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/Digits/day-09.wav;audio|file:///opt/product/tomcat/webapps/LycaIVRS_2.1
    .8.1_Rev0/promptFiles/audio/ORANGE/ENGLISH/Digits/year-2014.wav;
    GLISH/roamingAnnc.wav;”

    Above example is a single line. Like these many lines will be present in the file.
    Now I want to pick only “validityexpiry.wav”,”mon-01.wav”,”year-2014.wav” basically the words that are ending with .wav
    I tried with egrep -o “*.wav;” pw_metricsfile –> but its printing only “.wav;” as output
    Can some body suggest how can i exactly pick the word i want.

    Thanks inadvance

    • jetole Jan 11, 2013 @ 10:25

      I named the file test.txt and used the command grep -io ‘[^/]\+\.wav’ test.txt

      • Rao Jan 11, 2013 @ 11:32

        Thanks a lot Jetole…its working like a charm….can you please explain the syntax usage for my better understanding.

        • jetole Jan 11, 2013 @ 12:00

          I’ll give you the quick and dirty answer but this is regex which grep relies on so I would make a point of learning that. A good place to start would be man 7 regex or the regex book from O’Reilly.

          • Anyways, anything between [ and ] match any character between them so [abc] will match a or b or c [a-z] will match any lower case letter, [a-zA-Z0-9. ] will match any letter, number, period or space.
          • Preceding the match with ^ inverts it so [^abc] will match anything except a or b or c. [^/] says match anything except a forward slash.
          • + says match at least one or more occurrences but it needs to be escaped to work otherwise it will match a literal + instead of the regex action. It’s similar to * but * is 0 or more.

          • A period (.) is a special character in regex that says match any character so if I want to search for a literal period instead of regex for any character I have to escape it with a bask slash.

          Combine all of this together and it matches any string preceding “.wav” and the “.wav” that does not contain a forward slash. If there is a forward slash then it starts right after it. Hope this helps.

          • Rao Jan 11, 2013 @ 12:30

            Thanks again…it was a great learning.. :-)

  • Rao Jan 11, 2013 @ 13:12

    @Jetole: Sorry to bother you…when I run the command on single file its working fine.
    I have around 700 files to search for. when I searched for multiple files,file name is being prefixed to some of the output strings.

    grep -io ‘[^/]\+\.wav’ pw_metricsfile*
    O/p:
    pw_metricsfile:validityexpiry.wav
    mon-01.wav
    day-09.wav
    year-2014.wav
    promoBalance.wav
    1.wav
    Thousand.wav
    9.wav
    Hundred.wav
    73.wav
    Rupees.wav
    and.wav
    91.wav
    Paise.wav
    roamingAnnc.wav
    pw_metricsfile:bundlewithoutcustcare.wav
    pw_metricsfile:WelcomePrompt.wav
    pw_metricsfile:YourBalanceTalkTime.wav
    1.wav
    Thousand.wav
    9.wav
    Hundred.wav
    85.wav
    Rupees.wav
    and.wav
    46.wav
    Paise.wav
    pw_metricsfile:plzenterbundleno.wav
    pw_metricsfile:YouHaveEntered.wav

    You might have observed that file name “pw_metricsfile” is being prefixed to output.
    I managed by piping grep -v “pw_metricsfile” ,but just wanted to check is there some ways we still need to modify the command to overcome this issue ??

    • jetole Jan 11, 2013 @ 13:16

      I’m on my phone at the moment so all I can say is run man grep. I know the option is there but don’t remember what it is.

  • Jan Jan 11, 2013 @ 22:51

    Rao:
    assuming you don’t want that preceding filenames:

    from ‘man grep’:
    -h, –no-filename
    Suppress the prefixing of file names on output. This is the
    default when there is only one file (or only standard input) to
    search.

    • Rao Jan 15, 2013 @ 5:19

      Sorry for the late reply as it was a local holiday here….
      Thanks again…with “-h” option its working perfect ….kudos to you :-)

  • mohit Mar 28, 2013 @ 23:39

    I fond this tutorial very clear and simple to understand.. Thanks

  • Rosy Mar 29, 2013 @ 11:36

    I have a question. I have a log file having thousands of lines where i want to extract a piece of information. I do not know at what line number the information will appear.

    i have pattern 1 and i am able to find the line no for this by using:

    start=$(cat -n nommsu.log | grep -n “PNAC ” | head -1 | cut -f1 -d’:’)

    There is pattern 2 that appears for every pattern 1 and i want to extract the line number for that. Please help me with that.

    Finally, i want to use the following :

    sed -n $start,$endp filename > newfile
    so that i can ftp newfile locally to my desktop and perform some operations.

    Thanks,
    Rosy

  • James King Apr 10, 2013 @ 5:21

    Can you give me some CLI commands for GREP to filter out lines from a story that contains an HTML header but the rest of the file is an ordinary text file? I figure the lines with an ‘<' would work. I did this in DOS with the FIND command but don't make the connection with GREP.

    Thanks

    • jetole Apr 15, 2013 @ 23:53

      James King, I would use the tail command for that. Something in the context of ` tail -n +5 myfile.HTML.txt` to only show from live 5 down.

  • Akshay Sahu Apr 15, 2013 @ 10:14

    Very simple, lucid tutorial. Thanks a lot writer.

    Thanks,
    Akshay Sahu

  • ashok Apr 19, 2013 @ 8:38

    Hi ,

    In linux I want to display all files in the current directory from 19/04/13 to 20/04/13
    dates by using awk.
    But in linux the files are getting sorted and th output would be like
    files from dates ??/04/13(?? given below)
    19
    02
    20

    But i need only files on 19 and 20 but not 02. please let me know how can we rectify this.

    • jetole Apr 20, 2013 @ 4:17

      I don’t know about how awk is being used and you didn’t give an example but one way to find the files is via find. You can use the -newermt option which file files with the modified date newer then the pattern you provide.

      find . -type f -newermt ‘Apr 18 23:59:59 EDT 2013’ ! -newermt ‘Apr 19 23:59:59 EDT 2013’

      This statement says find all files (not directories with the -type f option but you can remove -type to match files and directories and more), in my current directory and all subdirectories (period(.) means current dir that are newer then the last second on April 18th’s which would be the first second on April 19th. Then it uses the option to find all files newer then the last second on April 19th but it has an exclamation point in front of it which means anything this option matches it will not return. Exclamation(!) negates an option so any option starting with a ! is saying don’t return anything this option matches This will find all files on April 19th 2013. It will not return files from April 18th or April 20th.

      If for some reason you need the ls -l output, you can run this:

      find . -type f -newermt ‘Apr 18 23:59:59 EDT 2013’ ! -newermt ‘Apr 20 00:00:00 EDT 2013’ -exec ls -l ‘{}’ \;

      The -exec option says to execute this command each time we find a file and {} is what the matched file is so you place that where you would type in the file if running this on a command line. -exec needs to be told where your command ends via the \; at the end.

      Hope this helps and oh! …a few other things:

      1. I used the last 23:59:59 aspect because if I used 00:00:00 then it would not find files from midnight on the dot on the 19th and would include files from midnight on the dot on the 20th.
      2. You can use short form of dates but how you printed the date, which I do prefer, is different then the locale where I am and I didn’t want to mess up the example. To quote the find man page “Time specifications are interpreted as for the argument to the -d option of GNU date”
      3. bash and shell scripting rule of thumb is never try to parse ls in any way. It’s not really a parse friendly result. Take a look at http://mywiki.wooledge.org/ParsingLs
  • jetole Apr 20, 2013 @ 4:19

    Oops! My second example was meant to be

    find . -type f -newermt ‘Apr 18 23:59:59 EDT 2013′ ! -newermt ‘Apr 19 23:59:59 EDT 2013′ -exec ls -l ‘{}’ \;

    I copied and pasted the wrong line from the terminal I was testing in. Sorry about that.

    • ashok Apr 23, 2013 @ 10:49

      HI Jetole,

      Thanks for the explanation.
      I had another case for example:
      In lunux , the following is displayed when we use ls -lt |more
      -rw-r–r– 1 ashok usr 2572 Apr 17 12:18 file1.prn
      -rw-r–r– 1 ashok usr 126 Apr 8 16:01 file2.prn
      -rw-r–r– 1 ashok usr 3778 Apr 8 15:41 file3.prn
      -rw-r–r– 1 ashok usr 1924 Apr 8 15:39 file4.prn
      -rw-r–r– 1 ashok usr 186 Apr 8 15:25 file5.prn
      -rw-r–r– 1 ashok usr 0 Apr 8 14:56 file6.prn
      -rw-r–r– 1 ashok usr 1151 Apr 8 10:57 ask.prn
      -rw-r–r– 1 ashok usr 19039 Apr 2 17:55 a1.prn
      -rw-r–r– 1 ashok usr 3057 Apr 2 17:51 a5.prn
      -rw-r–r– 1 ashok usr 265770 Apr 2 17:46 msg.prn
      -rw-r–r– 1 ashok usr 19575 Apr 2 17:24 d1.prn
      -rw-r–r– 1 ashok usr 3058 Apr 1 11:18 rep2.prn
      -rw-r–r– 1 ashok usr 175353 Mar 29 13:48 a2.prn

      Now if you observe the date Apr 2 , i want it to display it as Apr 02
      The result to be displayed as follows:
      -rw-r–r– 1 ashok usr 2572 Apr 17 12:18 file1.prn
      -rw-r–r– 1 ashok usr 126 Apr 08 16:01 file2.prn
      -rw-r–r– 1 ashok usr 3778 Apr 08 15:41 file3.prn
      -rw-r–r– 1 ashok usr 1924 Apr 08 15:39 file4.prn
      -rw-r–r– 1 ashok usr 186 Apr 08 15:25 file5.prn
      -rw-r–r– 1 ashok usr 0 Apr 08 14:56 file6.prn
      -rw-r–r– 1 ashok usr 1151 Apr 08 10:57 ask.prn
      -rw-r–r– 1 ashok usr 19039 Apr 02 17:55 a1.prn
      -rw-r–r– 1 ashok usr 3057 Apr 02 17:51 a5.prn
      -rw-r–r– 1 ashok usr 265770 Apr 02 17:46 msg.prn
      -rw-r–r– 1 ashok usr 19575 Apr 02 17:24 d1.prn
      -rw-r–r– 1 ashok usr 3058 Apr 01 11:18 rep2.prn
      -rw-r–r– 1 ashok usr 175353 Mar 29 13:48 a2.prn

      Could you please give any command for this……..

      • jetole Apr 23, 2013 @ 12:42

        No. Please read http://mywiki.wooledge.org/ParsingLs. If you want to tell me what you’re trying to do I might be able to help but if all you want to know is how to parse ls output then the answer is no.

  • Shefaya May 2, 2013 @ 7:27

    Very helpful, Thanxs

  • GANESH May 23, 2013 @ 18:20

    Hi
    Some one pease help me to grep and cut only virtual machine names from a file.

    File contects will be like below,i have n number of line,need to grep only which Virtual Machine which i marked ‘xxxx’ between quotes,also need to grep target node and data mover node names from the file,

    i wan the ouutput like 3 columns like
    Virtual machine name Target node Data mover
    “ANE4174E (Session: 129470, Node: NODE_NAME) Full VM backup of VMware Virtual Machine ‘XXXX’ failed with RC=65486 mode=Incremental Forever – Incremental, target node name=’NODE_NAME’, data mover node name=’NODE_NAME’ (SESSION: 129470)”
    “ANE4174E (Session: 129470, Node: NODE_NAME) Full VM backup of VMware Virtual Machine ‘XXXX’ failed with RC=65486 mode=Incremental Forever – Incremental, target node name=’NODE_NAME’, data mover node name=’NODE_NAME’ (SESSION: 129470)”
    “ANE4174E (Session: 129470, Node: NODE_NAME) Full VM backup of VMware Virtual Machine ‘XXXX’ failed with RC=65486 mode=Incremental Forever – Incremental, target node name=’NODE_NAME’, data mover node name=’NODE_NAME’ (SESSION: 129470)”
    “ANE4174E (Session: 129470, Node: NODE_NAME) Full VM backup of VMware Virtual Machine ‘XXXX’ failed with RC=65486 mode=Incremental Forever – Incremental, target node name=’NODE_NAME’, data mover node name=’NODE_NAME’ (SESSION: 129470)”

    Can any one please help to achive this.

  • apeksha Jun 4, 2013 @ 18:12

    Great recap! Thanks! :)

  • dinesh Rao Jul 31, 2013 @ 7:14

    grep ‘word’ filename
    grep ‘word’ file1 file2 file3
    grep ‘string1 string2’ filename
    cat otherfile | grep ‘something’
    command | grep ‘something’
    command option1 | grep ‘data’
    grep –color ‘data’ fileName
    Thanks & Regards
    Dinesh Rao
    Ph : 9729676868

  • Raghu Aug 6, 2013 @ 13:34

    Hi,
    someone please help on this,

    2012 Oct 02 09:05:09:937 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem User [BW-User] ESB-097 Job-36805 [Processes/Starters/FTP Location Monitor.process/FTP_ErrorLog]: FTP-ESB Error: Cannot perform FTP Operation: DIR. Error Info: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    Job-36805 Error in [Processes/Starters/FTP Location Monitor.process/FTP Dir]
    Cannot perform FTP Operation: DIR. Error Info: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    	at com.tibco.plugin.ftp.FTPDirActivity.eval(FTPDirActivity.java:210)
    	at com.tibco.pe.plugin.Activity.eval(Activity.java:241)
    	at com.tibco.pe.core.TaskImpl.eval(TaskImpl.java:574)
    	at com.tibco.pe.core.Job.a(Job.java:712)
    	at com.tibco.pe.core.Job.k(Job.java:501)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.a(JobDispatcher.java:249)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.run(JobDispatcher.java:200)
    caused by: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    	at com.tibco.plugin.ftp.FTPControlSocket.a(FTPControlSocket.java:455)
    	at com.tibco.plugin.ftp.FTPClient.list(FTPClient.java:2012)
    	at com.tibco.plugin.ftp.FTPTransport.ftpDir(FTPTransport.java:488)
    	at com.tibco.plugin.ftp.FTPDirActivity.eval(FTPDirActivity.java:192)
    	at com.tibco.pe.plugin.Activity.eval(Activity.java:241)
    	at com.tibco.pe.core.TaskImpl.eval(TaskImpl.java:574)
    	at com.tibco.pe.core.Job.a(Job.java:712)
    	at com.tibco.pe.core.Job.k(Job.java:501)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.a(JobDispatcher.java:249)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.run(JobDispatcher.java:200) 
    2012 Oct 02 09:05:09:938 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36805: Process Started 
    2012 Oct 02 09:05:09:950 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36805: Process Started 
    2012 Oct 02 09:05:09:958 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36805: Process Started 
    2012 Oct 02 09:05:09:961 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300023 Job-36805: Process Ended 
    2012 Oct 02 09:05:09:968 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: decrementJobCnt: jobCnt = 0 
    2012 Oct 02 09:10:09:013 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: received event: data = dm:node-kind()="document", dm:node-name()="null", dm:string-value()="13491834090129109401020122012-10-029:10:09 AM2", context = null 
    2012 Oct 02 09:10:09:014 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: added job Job-36806 to job pool: jobCnt = 1 
    2012 Oct 02 09:10:09:014 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36806: Process Started 
    2012 Oct 02 09:10:09:014 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: received event: data = dm:node-kind()="document", dm:node-name()="null", dm:string-value()="13491834090149109401020122012-10-029:10:09 AM2", context = null 
    2012 Oct 02 09:10:09:014 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: added job Job-36807 to job pool: jobCnt = 1 
    2012 Oct 02 09:10:09:014 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36807: Process Started 
    2012 Oct 02 09:10:09:351 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300023 Job-36806: Process Ended 
    2012 Oct 02 09:10:09:352 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Debug [BW-User]  from com.tibco.plugin.timer.TimerEventSource for process starter Timer: decrementJobCnt: jobCnt = 0 
    2012 Oct 02 09:10:09:971 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem User [BW-User] ESB-097 Job-36807 [Processes/Starters/FTP Location Monitor.process/FTP_ErrorLog]: FTP-ESB Error: Cannot perform FTP Operation: DIR. Error Info: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    Job-36807 Error in [Processes/Starters/FTP Location Monitor.process/FTP Dir]
    Cannot perform FTP Operation: DIR. Error Info: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    	at com.tibco.plugin.ftp.FTPDirActivity.eval(FTPDirActivity.java:210)
    	at com.tibco.pe.plugin.Activity.eval(Activity.java:241)
    	at com.tibco.pe.core.TaskImpl.eval(TaskImpl.java:574)
    	at com.tibco.pe.core.Job.a(Job.java:712)
    	at com.tibco.pe.core.Job.k(Job.java:501)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.a(JobDispatcher.java:249)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.run(JobDispatcher.java:200)
    caused by: Unexpected reply code. Returned Code: 550. Detail Description: File not found. Expected Code: 226,250.
    	at com.tibco.plugin.ftp.FTPControlSocket.a(FTPControlSocket.java:455)
    	at com.tibco.plugin.ftp.FTPClient.list(FTPClient.java:2012)
    	at com.tibco.plugin.ftp.FTPTransport.ftpDir(FTPTransport.java:488)
    	at com.tibco.plugin.ftp.FTPDirActivity.eval(FTPDirActivity.java:192)
    	at com.tibco.pe.plugin.Activity.eval(Activity.java:241)
    	at com.tibco.pe.core.TaskImpl.eval(TaskImpl.java:574)
    	at com.tibco.pe.core.Job.a(Job.java:712)
    	at com.tibco.pe.core.Job.k(Job.java:501)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.a(JobDispatcher.java:249)
    	at com.tibco.pe.core.JobDispatcher$JobCourier.run(JobDispatcher.java:200)
    2012 Oct 02 09:10:09:972 GMT -4 BW.GoldenHighwayiSeriesFileSystem-GoldenHighwayiSeriesFileSystem Info [BW-Core] BWENGINE-300007 Job-36807: Process Started 
    

    from above log, i want to grep the word “Error” and “followed by 4 lines after that word”.

  • Vishal Trivedi Aug 8, 2013 @ 5:03

    Hi,
    I want to add few characters in some particular files through terminal by using grep command. Foe example: I have few files which contains ::info. So I want to add // followed by ::info. So I can comment it. //::info Like this.
    Please help me on this.

  • jenny Aug 19, 2013 @ 17:45

    FAIL –
    When you search for boo, grep will match fooboo, boo123, barfoo35 and more.

  • jenny Aug 19, 2013 @ 17:49

    File “file” contains –

    fooboo, boo123, barfoo35
    boo, whoo, boohoo, booboo

    $ grep “boo” file
    fooboo, boo123, barfoo35
    boo, whoo, boohoo, booboo

    $ grep -w “boo” file
    boo, whoo, boohoo, booboo

  • Karuna Sep 25, 2013 @ 13:46

    Find character in vi editor

  • John Hsu Oct 4, 2013 @ 19:35

    Remove tons of abc files

    ll  | grep abc | awk '{print "rm "$8" "}' | csh

    Note:
    “$8” — column 8 of ll comand

    Thanks
    Sincerely
    John Hsu
    johnthsu at hotmail dot com

  • Manish K Oct 10, 2013 @ 15:39

    its wonderfullll…thx for ur efforts.

  • Susi Oct 17, 2013 @ 6:20

    ive been stuck i’m stuck trying to figure out how to display a numbers between 3.69 and 4.0. Its a list of GPA scores. Im trying to display the number above 3.69 and below 4.0.
    the code i’m using is :
    grep ‘[0-9]\{3.69-4.0\}’ smallFile

    if you can tell e what im doing wrong thank you

  • Dzmitry Nov 20, 2013 @ 19:33

    Thanks a lot. It’s very useful article

  • frank Dec 6, 2013 @ 0:16

    this tutorial is simple and clear. but very helpful. thanks

  • Prashant Mishra Jan 13, 2014 @ 9:36

    Nice one.. Saved my job

  • Antony James Jan 15, 2014 @ 10:12

    Nice

  • faran Jan 23, 2014 @ 7:11
    $(grep "full_ver=" $(which mex) | sed 's/[^0-9]//g')

    This is a command that is suppose to return my MATLAB version, but when I typed it in my shell there is no response. I wait too long but there was no answer. could any one help me with understanding this command?

    • Nix Craft Jan 23, 2014 @ 9:24

      Are you sue mex command installed? What is the output of the following commands?

      type -a mex
      which mex

      As far as I know the command name is matlab:

      matlab

      Sample outputs:

                                    < M A T L A B >
                        Copyright 1984-2005 The MathWorks, Inc.
                         Version 7.0.4.352 (R14) Service Pack 2
                                    January 29, 2005
      
      
        To get started, select MATLAB Help or Demos from the Help menu.
      • faran Jan 23, 2014 @ 10:32

        Thanks a lot. I found that there was a problem in my installation. I added these lines and my problem solved:
        sudo ln -s /usr/local/MATLAB/R2011a/bin/mbuild /usr/local/bin/mbuild
        sudo ln -s /usr/local/MATLAB/R2011a/bin/mcc /usr/local/bin/mcc
        sudo ln -s /usr/local/MATLAB/R2011a/bin/mex /usr/local/bin/mex

  • benjamin Jan 28, 2014 @ 13:32

    $ grep boo /etc/passwd -> Funny there is no boo in the sample output

  • blanche Feb 18, 2014 @ 14:36

    I am not sure if grep can do this or not but … I have a script and needed to go through an xml file and take the basic record count and divide it into threes. I was able to get everything I needed but when it comes down to getting only “x” amount of records to print in each file I’m lost….. any one can help that’d be great. Below is what i have for a script right now…

    #!/usr/bin/ksh
    set -x
    
    NUM_RECS=`sed -n '/Basic-Record-Count/{s/.*//;s/>File1Count.output
    else
    	eqAmt=`expr "$NUM_RECS" / "$NUM_FILES"`
    	remAmt=`expr "$NUM_RECS" % "$NUM_FILES"`
    	
    	echo "eqAmt: $eqAmt"
    	echo "remAmt: $remAmt"
    fi	
    	
    	if [ $remAmt -eq 1 ]
    	then
    		file1add=1
    		file2add=0
    	elif [ $remAmt -eq 2 ]
    	then
    		file1add=1
    		file2add=1
    	else
    		file1add=0
    		file2add=0
    	fi
    	
    	if  FILE1COUNT=`expr "$eqAmt" + "$file1add"` 
    	then
    		sed -n '/<MF-EXTRACT/,/>File1Count.output
    		sed -n '/<TRAILER/,/>File1Count.output
    	fi
    
    	if  FILE2COUNT=`expr "$eqAmt" + "$file2add"` 
    	then
    		sed -n '/<MF-EXTRACT/,/>File2Count.output
    		sed -n '/<TRAILER/,/>File2Count.output
    	fi
    		
    	if  FILE3COUNT=$eqAmt 
    	then
    		sed -n '/<MF-EXTRACT/,/>File3Count.output
    		sed -n '/<TRAILER/,/>File3Count.output
    	fi
    
    • Shridhar Apr 8, 2014 @ 7:30

      Hello Jyothi

      1)ps -ef will list the processes in the process table,
      2)in that grep -v use to invert the search, means here in the case grep -v grep will ignore the present one in process table.
      3)grep $orasid will search that pattern in the process table.
      4)Redirecting to /dev/null that means discarding.

  • Gifty Apr 7, 2014 @ 16:31

    really helpful…

  • Jim Wright Jun 26, 2014 @ 22:58

    grep -h suppresses the file name for multi-file greps. Is there a way to force the file name to show up even if the string is matched in only one file? I know I could create a loop with grep -l and do it that way, but I was hoping for something easier.
    grep -e string *.txt
    will return
    filename1.txt:string
    filename2.txt:string
    If found in two files, but if there is only one found it will return
    string

    I’m trying to parse the output and sometimes I get the file name and sometime not.

  • honey Jun 29, 2014 @ 11:21

    I have some questions pls reply;

    How to display the line that contains
    1: any number
    2: only file
    3: only directory
    4: only hidden character
    5: only special characters
    6: ends with 2 numbers
    7: contains any 2 numbers
    using grep

    if any one is having answer pls pls reply.

  • Thomas Jul 18, 2014 @ 11:47

    I have got the following grep command:

    # grep -r -l -h ‘hostgroup’ /omd/sites/test/etc/icinga/conf.d/objects/

    I got this output:

    /omd/sites/test/etc/icinga/conf.d/objects/test1/server/test01.cfg

    I only want the following output:
    “test01.cfg”

    I can´t use find, because I have to write the grep result via PHP-Function exec() into an array.

    I hope somebody can help me.

    • Gayathri Chakravarthy Aug 20, 2014 @ 15:20

      You can pass the output to basename, that should give you just the file name.
      test> a=/dummy/test.txt
      test> b=$(basename $a)
      test> echo $b
      test.txt

  • mohit bhardwaj Aug 11, 2014 @ 10:39

    I have a 300 msisdn in one flat file need to gerp these numbers state from another file..Canany one tell me what was the exact command for this problem

  • sachin Feb 26, 2015 @ 9:22

    Hi ,
    I want to GREP a value received console or file.

    while
    read line
    grep -c ‘$line’ file.csv
    do
    echo “$line”
    done < file.csv

    here I don't know the exact value that I have to GREP.I just take it from one file and compare it with other.

    Will this work??? How should I achieve this, if not from GREP any other commands.

    • Alex Egglestone Mar 17, 2015 @ 14:32

      I don’t know

  • Cillo Mar 1, 2015 @ 18:51

    I want to finde a no in a fine

    my line look like this “SWD_BUILD ?= 207 # can be 0-999- Build nr” and I only want 1 output. the no 207
    My command rigtht now is grep -A0 “SWD_BUILD” Makefile | grep -m1 -Eoe “[0-9][0-9][0-9]|[0-9][0-9]|[0-9]”
    But I get
    207
    0
    999

    How to get only the 207 out ??

  • sunilkumar Mar 19, 2015 @ 7:15

    Hi,

    I have one doubt .with out using (cat,vi touch) (don’t use the this symbol like)’>’
    nothing but (Redirection ).to create file in linux .what to do .
    but by using grep command.

    please tell me answer as soon as possible.

    Thanks & Regards
    sunilkuma

  • jetole Mar 19, 2015 @ 17:45

    Cillo,

    echo ‘SWD_BUILD ?= 207 # can be 0-999- Build nr’ | sed -e ‘s/#..*//’ | grep -o ‘[0-9]\+’

    The sed command removes from the point of the line beginning with the # to the end of the line. This means your line turns in to “SWD_BUILD ?= 207 ” before it is passed to grep.

  • Roger May 6, 2015 @ 17:42

    I am attempting to build a Disaster Recovery script that will restore a blank disk to a completely working copy of a failed disk on the same machine. So far, I have worked through all of the issues involved in getting everything backed up — including the LVM configuration.

    Now, I am trying to find a way to pull the UUID for the Physical Volume (pv0) from a backup lvm file to recreate the missing PV.

    I would like to auto insert that value in a script (pvcreate –restorefile –uuid uuid) to recreate the physical volume with the correct ID without having to manually enter it and possibly screw up the restore process.

    I have tried unsuccessfully to pull the UUID for the pv0 entry in the text snippet from the /etc/lvm/backup/fedora file.

    I would like to pull and filter the results so that ONLY the UUID for the id= value and the luks-234979blahblahblah value in the device= fields are returned.

    fedora {
    	id = "MwuUZW-mIYJ-zVdr-8ng4-12Bz-SUZl-nu3Gnp"
    	seqno = 10
    	format = "lvm2"			# informational
    	status = ["RESIZEABLE", "READ", "WRITE"]
    	flags = []
    	extent_size = 8192		# 4 Megabytes
    	max_lv = 0
    	max_pv = 0
    	metadata_copies = 0
    
    	physical_volumes {
    
    		pv0 {
    			id = "9k4kdu-oyKx-6Ur4-O7Wq-x59H-MUHR-fL7kkz"
    			device = "/dev/mapper/luks-48c6e5c1-b751-4868-b072-c3fc4ad6f715"	# Hint only
    
    			status = ["ALLOCATABLE"]
    			flags = []
    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite May 6, 2015 @ 18:17
      grep device filename | cut -d'/' -f4

      OR awk

      awk -F'/' '/device/{ print $4}' filename
      • Roger May 6, 2015 @ 19:04

        WoW! And so fast!! Thank you!!!
        I figured that I would start working on the blkid output that I have saved since it might be easier than working with the lvm backup file…

        Here is the critical line of data in the blkid output:

        /dev/mapper/luks-48c6e5c1-b751-4868-b072-c3fc4ad6f715: UUID=”9k4kdu-oyKx-6Ur4-O7Wq-x59H-MUHR-fL7kkz” TYPE=”LVM2_member”

        I need to set a variable that contains just the /dev/mapper/luks- value and…
        I need to set another variable that contains just the UUID for the same device — listed further down the same line…

        The variables that I would like to set are:
        LuksDevice
        PVUUID

        Thank you very much in advance for your assistance!
        Roger
        San Antonio, TX

  • Roger May 6, 2015 @ 18:51

    Regarding pulling UUID from LVM backup file…
    I guess I can also use a saved copy of blkid output for the same purpose, but I still can’t figure out how to pull the the actual UUID identifier from the file to be used as a variable…

    I REALLY appreciate ANY help you all can give me!

    Roger
    San Antonio, TX

  • Ron May 19, 2015 @ 17:36

    Good stuff.

  • Nandhini May 21, 2015 @ 23:32

    I wanted to grep the word “error” in a certain path using if else and send it to my mail. I am not sure how to proceed. If error is found, error needs to be displayed, else no error. Kindly help me with this.

    if [ egrep -inc error ]
    then
    #send e-mail with details
    cd /path/
    egrep -C10 -i error | failed | failure | mailx -s “Files Present” $DL
    exit 1
    else
    echo “No error found”
    fi

  • Kalpesh Patel May 22, 2015 @ 6:33

    Amazing… :) Really helpful….while making scripts with kornshell…. grep had been my hero for ages while filtering 10000’s of results from our servers…

    Thanks…

  • Karl May 24, 2015 @ 17:59

    Which switch is used to search any regular expression?

  • Amir Oct 31, 2015 @ 18:15

    Hi Friends, I am looking for help. Can you someone help me how to get below 2 output please.

    Input
    ——
    /usr/IBM/WebSphere/7.0/UpdateInstaller/java/jre/bin/java -Declipse.security -Dwas.status.socket=48101
    /home/axg009/usr/IBM/WebSphere/8.5/AppServer/java_1.7_64/jre/bin/java -Declipse.security -Dwas.status.socket=48101

    Output 1
    ——–
    7.0
    8.5

    Output 2
    ——–
    7.0/UpdateInstaller/java/jre/bin/java
    8.5/AppServer/java_1.7_64/jre/bin/java

  • shahroz khalid Nov 15, 2015 @ 8:21

    cat f1.txt | sort | grep abc
    i want to make that program in C… Any one help me??

  • Naren Nov 15, 2015 @ 16:12

    Hello All, I am looking for some help with unix commands using pipes and grep.

    I have a list of .csv files (a.csv, b.csv, c.csv, d.csv) in a directory and I have to get the names of the files in the directory and pass them one by one for importing into a sas dataset.

    I am using
    filename pipedir pipe ‘ls “Location of the directory” | grep “.csv” ‘;

    I am not able to get the names into the pipedir filename.

    Please help.

  • Ben Nov 22, 2015 @ 1:15

    Hi

    How do I search within only specific file types? i.e. grep -r “break;” but only in .m files?

    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Nov 22, 2015 @ 3:04

      grep -r "break" *.m
      OR
      find /path/to/dir/ -type f -name "*.m" -print0 | xargs -I {} -0 grep "break" "{}"

  • Joseph Tran Jan 29, 2016 @ 19:22

    How do i grep for words/strings in more than one line?
    Example:
    Line 10 contains the string “the cat”
    Line 11 contains the string “in the hat”
    I want to grep if the file contains the cat in the hat but not on same line.

    Thanks

  • vipin Jan 30, 2016 @ 11:21

    I have 2 files. File1 is zip and contains a list of long number with their corresponding informations. File2 contains only few number. I would like to find which information within file2 is present in file1 for that perticular number and extract the corresponding information.when i am trying below command i am getting an error “grep: illegal option –f”…. Please help…

    • Mex Jul 8, 2021 @ 11:29

      When working with the zip files the

      grep

      will not work. You need to use for this purpose an alternative called

      zgrep

      which greps into compressed files (with compression formats that

       gzip 

      recognizes).

      TLDR:

       zgrep -f file2 file1.gz 

      For demonstration purposes I prepared 2 files.
      File1.gz (compressed by

       gzip 

      ) containing many numbers and File2 containing just some. Because I wanted to do it quickly and did not find generator to generate comments after numbers in compressed file, I omitted this step.

      mex@mex-pc:~/test1/test4$ ls -alF
      total 60
      drwxr-xr-x 2 mex mex  4096 čec  8 13:04 ./
      drwxr-xr-x 5 mex mex  4096 čec  8 11:36 ../
      -rw-r--r-- 1 mex mex 49141 čec  8 13:03 file1.gz
      -rw-r--r-- 1 mex mex   296 čec  8 13:04 file2
      

      To see the content of the file:

      mex@mex-pc:~/test1/test4$ zcat file1.gz | head
      616368134
      812622967
      47051582
      268655354
      28200892
      557978709
      738748080
      472004485
      920270184
      19228452
      
      As we can see it has 10,000 lines there
       mex@mex-pc:~/test1/test4$ zcat file1.gz | wc -l
      10000
      

      File 2 which we want to use as a pattern list

      mex@mex-pc:~/test1/test4$ cat file2 | head
      444149151
      730607573
      802015991
      158262326
      914319599
      646182915
      425977201
      61573739
      433743449
      513213730
      
      mex@mex-pc:~/test1/test4$ wc -l file2
      30 file2
      

      Now we can use the

       zgrep 

      to search while using ptterns

      mex@mex-pc:~/test1/test4$ zgrep -f file2 file1.gz
      975274707
      832605167
      74410397
      794909767
      882932494
      74300429
      106408012
      700683966
      529518642
      44477198
      mex@mex-pc:~/test1/test4$ zgrep -f file2 file1.gz | wc -l
      10
       

  • Thant Zin Feb 2, 2016 @ 9:29

    please may i help you
    grep -e searchString fileName
    i can’t understand to write the C program .

  • rathod vicky Feb 3, 2016 @ 9:25

    this good information for grep commmand

  • Rama Mar 5, 2016 @ 20:24

    i want to search a “word” XYZ in a directory and copy the whole file from one AIX server to another server ..by using grep or find command. can any one help me

  • gil Apr 20, 2016 @ 22:22

    hi
    someone can help me with my problem?

    i have 20 files look like this:

    aaaa
    kkmk
    1 o
    2 x
    3 q
    ppp
    1 p
    2 b
    3 t

    i nees it search the last “ppp” and after this it copy the letter after “2”. and to do it in all files, when it complete i need to save it to txt file:
    “file name” “letter”
    “file name” “letter”
    “file name” “letter”

    thanks a lot!!

  • netikras Jul 1, 2016 @ 5:55

    This is one terribly long trail of comments…. not sure whether anyone will even see mine :) anyway, there is a nifty trick I’ve come up with recently – a way to display configuration files without all the junk (comments, empty lines, etc…). try it – much easier to read!

    cat /etc/minidlna.conf | grep -v "^#" | grep -v "^[ \t]*$"

    (yeah, I know… cat’ing and grepping is ineffective, blah blah. But it makes sense and saves *your* time rather than computer’s. Which one is more expensive huh?)

  • anupama Jul 14, 2016 @ 5:30

    helooooo.,,,
    Very much helpfullll
    :)

  • juliomatcom Jul 27, 2016 @ 9:15

    $ man grep
    for more info

  • Manoj Jul 27, 2016 @ 15:42

    Hello,

    How can i get multiple output files using grep/awk.

    eg: grep “word1\|word2\|word3” > filename. what i required here is fileword1, fileword2 and fileword3.

    Please help.

  • Peet Sep 21, 2016 @ 7:35

    Hi I’m trying to get info. from log file, having this command below. When I pull the command the output is with 10x “0”. I need to count TPS for those hosts to get know how many times they appeard in the 10 min. time-frame. Anyone knows? I should use the “.sh” to make it script, but don’t know how to.

    for host in ` cat access.log-20160916 | grep "11:0" | cut -f8 -d" " | sort -u`
    do
      echo -ne "$host;"
      for minute in {00..09}
      do
        count=`cat access.log-20160916 | grep '2016-09-16 11:0' | grep "2016-09-16 11:$minute" | grep $host | wc -l`
        echo -ne "$count;"
      done
      echo ""
    done
    
  • Jacob Oct 12, 2016 @ 9:55

    So many comments wtf O_o

  • bob Oct 12, 2016 @ 12:55

    how do i use the grep command to…
    1) show the contents as automatically numbered paragraphs
    2) show only the last paragraph

  • Bryan Veloso Sep 23, 2020 @ 6:00

    What if I want to search one folder, but exclude only one specific sub-folder inside that folder that I search?

    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Sep 23, 2020 @ 11:21

      find is a better solution. For example, search for folder1 but ignore folder2
      find /path/to/search/ -name "folder1" | grep -v "folder2"

    • Mex Jul 8, 2021 @ 9:52

      If I understood your question correctly, solution to your question is

       grep -ir --exclude-dir=folder2  "regex_to_search" folder1 

      it is possible to use it also without

       -i 

      switch if you do not want it case insensitive.

      Unfortunately it is not possible to post the printscreen therefore I am adding the copy of test from my terminal for demonstration if this satisfy your question.

      mex@mex-pc:~$ grep -ir  "password" test1
      test1/test2/passtest:password
      test1/test2/passtest:password2
      test1/test2/passtest:password3
      test1/test2/passtest:Password
      test1/test3/filewithpasstoo:password
      test1/test3/filewithpasstoo:Password
      test1/file:password
      
      mex@mex-pc:~$ grep -ir --exclude-dir=test2  "password" test1
      test1/test3/filewithpasstoo:password
      test1/test3/filewithpasstoo:Password
      test1/file:password
      
  • anand j Sep 24, 2020 @ 10:02

    Hi All,
    I have a log file i want to filter between dates and apply AND condition on matching multiple patterns, if either one of the pattern mis-matches the result should be empty.

  • David Marcus Mar 9, 2022 @ 15:59

    Useful but also useless with respect to what I wanted to find out.
    There is NO treatment of escape characters.

    I want to create a bash shell the greps for the string:

        [abc xyz(

    where the ‘[‘ and the ‘{‘ are to be treated as plain characters.

    I could not find anything here that helps :-(

    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Mar 9, 2022 @ 17:22

      You need to use the fgrep syntax:

      fgrep '[abc xyz(' filename
      grep -F '[abc xyz(' filename
      

      To match a character that is special to grep -E (or egrep), put a backslash (\) in front of the character:

      grep -E '\[abc xyz\(' filename
      egrep '\[abc xyz\(' filename

      HTH

  • Anonymous Jan 15, 2023 @ 18:01

    Noice page and very help for new person like me.

  • Rana Jan 19, 2023 @ 10:43

    How to find the count of “aba” in “ababa” as 2 using grep command?
    echo “ababa” | grep -c “aba” results 1. Can you please help me how to get it as 2?

  • Lakshitha Feb 13, 2023 @ 4:48

    Hi

    I need to display 20 lines available in the log after filed ‘Field 039: 911’. below command will display only one line. How to display 20 lines ?

    tail -f |grep -wns 'Field 039: 911' LISRVR_ATM_SWIF_20421_100223_08.log -A 20
    Thanks

    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Feb 13, 2023 @ 5:19

      The tail command output the last part of files. You need to replace tail with cat command or directly pass the file. For example:
      grep -wns 'Field 039: 911' LISRVR_ATM_SWIF_20421_100223_08.log -A 20

      • Lakshitha Feb 14, 2023 @ 3:51

        Dear Vivek
        Thanks for replying
        with cat command it only display one line like below
        NADM > cat |grep -wns ‘Field 039: 911’ LISRVR_ATM_SWIF_20421_100223_08.log -A 20
        LISRVR_ATM_SWIF_20421_100223_08.log:38322:Field 039: 911

        • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Feb 14, 2023 @ 8:16

          No, when you cat avoid giving file name in grep:
          cat LISRVR_ATM_SWIF_20421_100223_08.log | grep -wns -A 20 'Field 039: 911'
          ## OR ##
          grep -wns -A 20 'Field 039: 911' LISRVR_ATM_SWIF_20421_100223_08.log

          • Lakshitha Feb 14, 2023 @ 9:15

            Is this happen due to OS version?

            ADM > cat LISRVR_ATM_SWIF_20421_100223_08.log |grep -wns -A 20 ‘Field 039: 911’
            grep: illegal option — A
            Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
            grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]… [-f pattern_file]… [file…]
            grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
            grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]… [-f pattern_file]… [file…]
            grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
            grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]… [-f pattern_file]… [file…]

            —————————————————————————————————————————
            ADM > grep -wns -A 20 ‘Field 039: 911’ LISRVR_ATM_SWIF_20421_100223_08.log
            grep: illegal option — A
            Usage: grep [-c|-l|-q] [-bhinsvwx] pattern_list [file ...]
            grep [-c|-l|-q] [-bhinsvwx] [-e pattern_list]… [-f pattern_file]… [file…]
            grep -E [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
            grep -E [-c|-l|-q] [-bhinsvx] [-e pattern_list]… [-f pattern_file]… [file…]
            grep -F [-c|-l|-q] [-bhinsvx] pattern_list [file ...]
            grep -F [-c|-l|-q] [-bhinsvx] [-e pattern_list]… [-f pattern_file]… [file…]

            • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Feb 14, 2023 @ 9:54

              May I know your OS and grep version? It is possible that your version of grep does not support the advance `-A` option.

              • Lakshitha Feb 15, 2023 @ 3:28

                its solaris
                SunOS prnlfincoreapp01 5.11 11.3 sun4v sparc sun4v

                • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Feb 15, 2023 @ 9:48

                  Your version of grep is old and does not support advanced option such as `-A`. Some folks have gnu grep installed on that OS at /usr/sfw/bin/ggrep location. Try that version using full path:
                  cat LISRVR_ATM_SWIF_20421_100223_08.log | /usr/sfw/bin/ggrep -wns -A 20 'Field 039: 911'

  • Lakshitha Feb 16, 2023 @ 5:04

    Great….perfectly working Vivek. Many Thanks. Any possibility to refresh the log. (Just like a tail command)

  • grep Aug 4, 2023 @ 11:05

    grep -F California address.txt
    such as “Californication” or “Californian”

  • grep23 Aug 4, 2023 @ 11:12

    grep -F California address.txt
    such as “Californication” or “Californian”

    California
    Californic ation // this won’t match
    California n

  • Dov Dec 23, 2023 @ 23:08

    This was great!!
    Always used cat file | grep -i word

    Time to kill the cat.

    Thank you!

  • Pari Jan 30, 2024 @ 15:42

    How write in linux for this( All directories having files at level 1)

  • Badger Patcher Apr 17, 2024 @ 10:22

    Thanks.

  • Nohtyp Jun 27, 2024 @ 9:05

    It was helpfull, thank you very much for your effort:D

  • UserFromTwitter Jul 28, 2024 @ 0:27

    Help me slot thanks

  • Anonymous Sep 8, 2024 @ 11:06

    Oh my, over 300 comments dating back to the 2000s. This is the original Stack Overflow for Linux developers.

  • Ben Oct 17, 2024 @ 6:50

    Hi! Help me, please
    I need to find in the compressed file (located in the home directory) files containing the word “program” inside.This word can be anywhere in the text in any case, even inside another word. How to search if it’s a binary file?

    • 👮🛡️ Vivek Gite (Author and Admin) Vivek Gite Oct 17, 2024 @ 8:42

      For compressed files, try using zgrep. To recursively search for a word named ‘foo’ in /tmp/dir1/ and all its subdirectories, try:

      zgrep -r 'foo' /tmp/dir1'

Leave a Reply

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.