Chapter 18
Looking Inside Files (grep, select-string)

18.1 Do This

I'm showing you a little trick for typing text into a file very quickly. If you do cat > somefile.txt then cat will read whatever you type and then write it to that file. On Windows it's done with echo > somefile.txt. The important thing, though, is that you have to "close" the file by typing CTRL-d.

If you can't figure this out just use a text editor to make the newfile.txt and oldfile.txt.


Source 33: Linux/Mac OSX Exercise 18
  $ cd temp
  $ cat > newfile.txt
  This is a new file.
  This is a new file.
  This is a new file.
  $ cat > oldfile.txt
  This is a old file.
  This is a old file.
  This is a old file.
  $ grep new *.txt
  newfile.txt:This is a new file.
  newfile.txt:This is a new file.
  newfile.txt:This is a new file.
  $ grep old *.txt
  oldfile.txt:This is a old file.
  oldfile.txt:This is a old file.
  oldfile.txt:This is a old file.
  $ grep file *.txt
  newfile.txt:This is a new file.
  newfile.txt:This is a new file.
  newfile.txt:This is a new file.
  oldfile.txt:This is a old file.
  oldfile.txt:This is a old file.
  oldfile.txt:This is a old file.
  $

In Windows a similar trick is to do echo > somefile.txt and then you'll be prompted for each line. Give an empty line to stop entering text.


Source 34: Windows Exercise 18
  > cd temp
  > echo > newfile.txt
  
  cmdlet Write-Output at command pipeline position 1
  Supply values for the following parameters:
  InputObject[0]: This is a new file.
  InputObject[1]: This is a new file.
  InputObject[2]: This is a new file.
  InputObject[3]:
  > echo > oldfile.txt
  
  cmdlet Write-Output at command pipeline position 1
  Supply values for the following parameters:
  InputObject[0]: This is a old file.
  InputObject[1]: This is a old file.
  InputObject[2]: This is a old file.
  InputObject[3]:
  > select-string new *.txt
  
  newfile.txt:1:This is a new file.
  newfile.txt:2:This is a new file.
  newfile.txt:3:This is a new file.
  
  
  > select-string old *.txt
  
  oldfile.txt:1:This is a old file.
  oldfile.txt:2:This is a old file.
  oldfile.txt:3:This is a old file.
  
  
  > select-string file *.txt
  
  newfile.txt:1:This is a new file.
  newfile.txt:2:This is a new file.
  newfile.txt:3:This is a new file.
  oldfile.txt:1:This is a old file.
  oldfile.txt:2:This is a old file.
  oldfile.txt:3:This is a old file.
  
  
  >

18.2 You Learned This

You made two files that were almost the same, except one had "new" and the other had "old" in it. Then you searched for different words in those files. You can look for words, but you can also find whole sentences in files if you put them in quotes.

18.3 Do More

  1. Use quotes to find "new file" and "old file" and "This is".
  2. Take the list of videos you created (or any other list) and use it to find some videos you want to find.
  3. Unix: You can use -i to ignore case with grep. Try grep -i new *.txt


Online Video Course + PDF For $9

For the price of most other course's PDFs only, you can get the full PDF for this class and 2 videos demonstrating the whole book for both Unix/OSX Terminal and Windows PowerShell. The course is self-paced so you can go through it any time you want, as many times as you want.


Signup Now At Udemy.com For $9