squirrelworks

Grep demo

The grep command is a powerful command-line utility used to search for specific patterns in text and then print the lines that match those patterns. The name grep is an acronym for "globally search for a regular expression and print it out".

Let's work through some exercises by Sundeep at github.com/learnbyexample
Sample.txt
Hello World

Hi there
How are you

Just do-it
Believe it

banana
papaya
mango

Much ado about nothing
He he he
Adios amigo


Code.txt

fruit = []
fruit[0] = 'apple'
def fruit():
    print('banana')


1. Display lines containing an .
grep 'an' sample.txt
banana
mango

2. Display lines containing do as a whole word.
grep -w 'do' sample.txt
Just do-it

3. Lines that meet these conditions:

  • he matched irrespective of case
  • eitherWorld or Hi matched case senstively
$ grep -i 'he' sample.txt | grep -e 'World' -e 'Hi'
Hello World
Hi there

4. Lines containing fruit[0] literally.
$ grep -F 'fruit[0]' code.txt

fruit[0] = 'apple'


5. Display only the first two matching lines containing t from the sample.txt input file.
$ grep -m2 't' sample.txt
Hi there
Just do-it

6. Display only the first three matching lines that do not containhe
$ grep -m3 -v 'he' sample.txt
Hello World

How are you

7. Lines that contain do along with line number prefix.
$ grep -n 'do' sample.txt
6:Just do-it
13:Much ado about nothing

8. Count the number of times the string he is present, irrespective of case.
$ grep -io 'he' sample.txt | wc -l
5

9. Count the number of empty lines
$ grep -cx '' sample.txt
4

10. For both input files - display matching lines based on the search terms (one per line) present in the terms.txt file. Results should be prefixed with the corresponding input filename.
terms.txt are not go fruit[0]

$ grep -Ff terms.txt sample.txt code.txt
sample.txt:How are you
sample.txt:mango
sample.txt:Much ado about nothing
sample.txt:Adios amigo
code.txt:fruit[0] = 'apple'



to be continued....


Sources
  1. github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/exercises/Exercise_solutions.md
  2. redhat.com/en/blog/how-to-use-grep


Accessibility
 --overview

Agile
 --DevOps overview
 --Principles

API
 --REST best practices
 --REST demo
 --REST vs RPC
 --Wikipedia API

Blockchain
 --overview

Cloud
 --AWS overview

CSS/HTML
 --Bootstrap carousel
 --Grid demo
 --markdown demo

Electricity
 --fundamentals

Encoding
 --Overview

Ergonomics
 --Desk configuration
 --Device fleet
 --Input device array
 --keystroke mechanics
 --Phones & RSI

ERP
 --Anthology overview
 --Ellucian Banner
 --Higher Ed ERP Simulation Lab
 --PeopleSoft Campus Solutions
 --PESC standards
 --Slate data model

Git
 --syntax overview
 --troubleshooting libcrypto

Hardware
 --Device fleet
 --Homelab diagram

Java
 --Fundamentals

Javascript
 --Advanced Interaction: jQuery & UI Frameworks
 --input prompt demo
 --misc demo
 --Time and Date functions
 --Vue demo

Linux
 --grep demo
 --HCI and Proxmox
 --Proxmox install
 --xammp ftp server

Mail flow
 --DKIM, SPF, DMARC
 --MAPI

Microsoft
 --AZ-800: Administering Windows Server Hybrid Core Infrastructure
 --BAT scripting
 --Group Policy
 --IIS
 --robocopy
 --Server 2022 setup - Virtualbox

Misc
 --Applications
 --regex
 --Resources
 --Sustainable Computing
 --Terminology
 --Tribute to Computer Scientists

Networks
 --BGP Peering & Security Hardening Lab
 --CCNA Lammle Study Guide
 --Cisco 1921/K9 router
 --routing protocols
 --throughput calculations

PHP/SQL
 --Cookies
 --database interaction
 --demo, OSI Layers quiz
 --Foreign key constraint demo
 --fundamentals
 --MySQL and PHPmyAdmin setup
 --pagination
 --security
 --session variables
 --SQL fundamentals
 --structures
 --Tables display

Python
 --fundamentals

Security
 --Overview- GRC (Governance, Risk, and Compliance)
 --Security Blog
 --SSH fundamentals

Serialization
 --JSON demo
 --YAML demo