Nic Acton
  • My Gitbook
  • My Favorite Things
    • Podcasts
    • Newsletters
  • Monthly Summaries
    • May 2019
    • June 2019
  • Cloud Computing
    • Cloud Concepts
    • AWS
      • Certified Solutions Architect
      • Well Architected Framework
        • Operational Excellence
        • Reliability
        • Performance Efficiency
        • Cost Optimization
        • Security
      • Analytics
        • Elasticsearch Service
        • Kinesis
        • Elastic MapReduce (EMR)
      • Compute Services
        • Elastic Beanstalk
        • Elastic Container Service (ECS)
      • Deployment
        • CloudFormation
      • Application Services
        • Key Management Service (KMS)
        • Simple Queue Service (SQS)
        • API Gateway
        • Simple Work Flow (SWF)
        • Amazon MQ
        • Simple Notification Service (SNS)
      • Simple Storage Service (S3)
        • Macie
      • Databases
        • RDS
        • DynamoDB
        • ElastiCache
        • Neptune
        • Redshift
      • Cloudfront
      • IAM
      • Monitoring
        • Trusted Advisor
        • Amazon Inspector
        • AWS Config
        • AWS Shield
        • CloudWatch
          • VPC Flow Logs
        • CloudTrail
        • Guard Duty
      • Route53
      • Serverless Architectures
        • Lambda
      • VPC
        • Highly Available & Fault Tolerant VPCs
        • Hybrid Environments
          • VPC Peering
          • Direct Connect
        • Cloud HSM
    • GCP
    • Azure
    • HashiCorp
    • Red Hat
      • RHEL
        • Basics
        • Grep & Regex
        • SSH
      • Ansible
    • Tutorials/Guides
      • Linux
        • Admin
  • Software Engineering
    • Machine Learning
      • Deep Learning
        • Tensorflow
      • Training and Loss
    • Programming
      • APIs
    • Security
    • Web Development
      • OSI 7 Layer Model
    • Tutorials/Guides
      • Apache Server
    • Virtualization
      • Virtual Machines
      • Containers
      • Serverless
  • Fitness
    • Nutrition
      • Diets
      • Macronutrients
      • Supplements
      • Miscellaneous
    • Strength Training
    • BodyBuilding
  • Miscellaneous
    • Technology Ethics
      • Education
    • Interesting Concepts
      • Libertarian Paternalism
Powered by GitBook
On this page
  • Grep
  • Regular Expressions (Regex)

Was this helpful?

  1. Cloud Computing
  2. Red Hat
  3. RHEL

Grep & Regex

Grep

A program for finding matching patterns. Follows $ grep <pattern> <file> or as part of a pipe: $ find . -name *.txt | grep taxes

Common Flags:

  • -i : makes it case insensitive

  • -r : makes the search recursive (searches directories)

  • -v : makes the search find all instances where there is not a pattern match

  • -w : makes the search match on a word rather than any pattern match

Regular Expressions (Regex)

Regex is passed into grep like so grep '<expression>' file

  • ^ - match expression at the start of a line, as in ^A

  • $ - match expression at the end of a line

  • \ - Turn off the special meaning of the next character and treat it as a string, as in \* or \^

  • [ ] - Match any one of the enclosed characters, as in [aeiou] or a range like [0-9] or [a-zA-Z]

  • [^ ] - Match any one character except those enclosed in [ ] as in [^0-9]

  • . Match a single character of any value except EOL

  • * - Match zero or more of the preceding character or expression

  • \{x,y\} - Match x to y occurences of the preceding

  • \{x\} - Match exactly x occurrences of the preceding

  • \{x,\} - Match x or more occurences of the preceding

Grep Examples

  • grep linux files - Search files for lines with the word linux

  • grep '^linux' files with linux at the start of a line

  • grep 'linux$' files with linux at the end of a line

  • grep '^linux$' files with lines containing only linux

  • grep '\^s' files lines starting with ^s (\ is an escape for that character)

  • grep '[Ll]inux' files search for either Linux or linux

  • grep 'B[oO][bB]' files search for BOB, Bob, BOb or BoB

  • grep '^$' files search for blank files

  • grep '[0-9][0-9]' files search for pairs of numeric digits

PreviousBasicsNextSSH

Last updated 5 years ago

Was this helpful?