InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Sifting Through Files for a String in bash
Bash

Sifting Through Files for a String in bash

InfinityCoder February 19, 2017

You need to find all occurrences of a string in one or more files.

The grep command searches through files looking for the expression you supply:

1
2
3
4
5
6
7
$ grep printf *.c
both.c:    printf("Std Out message.\n", argv[0], argc-1);
both.c:    fprintf(stderr, "Std Error message.\n", argv[0], argc-1);
good.c:    printf("%s: %d args.\n", argv[0], argc-1);
somio.c:         // we'll use printf to tell us what we
somio.c:         printf("open: fd=%d\n", iod[i]);
$

The files we searched through in this example were all in the current directory.

We just used the simple shell pattern *.c to match all the files ending in .c with no preceding pathname.
Not all the files through which you want to search may be that conveniently located.
Of course, the shell doesn’t care how much pathname you type, so we could have done something like this:

1
$ grep printf ../lib/*.c ../server/*.c ../cmd/*.c */*.c

When more than one file is searched, grep begins its output with the filename, followed by a colon.

The text after the colon is what actually appears in the files that grep searched.
The search matches any occurrence of the characters, so a line that contained the string “fprintf” was returned, since “printf” is contained within “fprintf”.
The first (non-option) argument to grep can be just a simple string, as in this example, or it can be a more complex regular expression (RE).

These REs are not the same as the shell’s pattern matching, though they can look similar at times.

Pattern matching is so powerful that you may find yourself relying on it to the point where you’ll start using “grep” as a verb, and wishing you could make use of it everywhere, as in “I wish I could grep my desk for that paper you wanted.”
You can vary the output from grep using options on the command line.

If you don’t want to see the specific filenames, you may turn this off using the -h switch to grep:

1
2
3
4
5
6
7
$ grep -h printf *.c
printf("Std Out message.\n", argv[0], argc-1);
fprintf(stderr, "Std Error message.\n", argv[0], argc-1);
printf("%s: %d args.\n", argv[0], argc-1);
   // we'll use printf to tell us what we
   printf("open: fd=%d\n", iod[i]);
$

If you don’t want to see the actual lines from the file, but only a count of the number of times the expression is found, then use the -c option:

1
2
3
4
5
$ grep -c printf *.c
both.c:2
good.c:1
somio.c:2
$

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using More Than Just a Constant String for Default in bash
You need something more than just a constant string as …

Using More Than Just a Constant String for Default in bash

Promoting Script Readability in bash
You’d like to make your script as readable as possible …

Promoting Script Readability in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap