Thamizhiniyan C S
HomeWriteupsResourcesCheatsheets
HackerRank
HackerRank
  • HackerRank
  • Linux Shell
    • Bash
    • Text Processing
      • Cut
      • Head
      • Middle
      • Tail
      • Tr
      • Sort
      • Uniq
      • Paste
    • Arrays In Bash
    • Grep Sed Awk
      • Grep
      • Sed
      • Awk
  • Regex
    • Introduction
    • Character Class
    • Repetitions
    • Grouping and Capturing
    • Backreferences
    • Assertions
    • Applications
  • Python
    • Introduction
    • Basic Data Types
    • Strings
    • Sets
    • Math
    • Itertools
    • Collections
    • Date and Time
    • Errors and Exceptions
    • Classes
    • Built-Ins
    • Python Functionals
    • Regex and Parsing
    • XML
    • Closures and Decorators
    • Numpy
    • Debugging
  • C
    • Introduction
    • Conditionals and Loops
    • Arrays and Strings
Powered by GitBook
On this page
  • Regexp Cheatsheet
  • 'Grep' #1
  • 'Grep' #2
  • 'Grep' #3
  • 'Grep' - A
  • 'Grep' - B

Was this helpful?

  1. Linux Shell
  2. Grep Sed Awk

Grep

PreviousGrep Sed AwkNextSed

Last updated 1 year ago

Was this helpful?

Regexp Cheatsheet

'Grep' #1

You are given a text file that will be piped into your command through STDIN. Use grep to display all the lines that contain the word the in them. The search should be sensitive to case. Display only those lines of the input file that contain the word 'the'.

grep -w the

'Grep' #2

You are given a text file that will be piped into your command through STDIN. Use grep to display all those lines that contain the word the in them. The search should NOT be sensitive to case. Display only those lines of the input file that contain the word 'the'.

grep -i -w the

'Grep' #3

You are given a text file that will be piped into your command through STDIN. Use grep to remove all those lines that contain the word 'that'. The search should NOT be sensitive to case.

grep -i -w -v that

'Grep' - A

Given a text file, which will be piped to your command through STDIN, use grep to display all those lines which contain any of the following words in them: the that then those The search should not be sensitive to case. Display only those lines of an input file, which contain the required words.

grep -E -i -w "the|then|that|those"

'Grep' - B

Given an input file, with N credit card numbers, each in a new line, your task is to grep out and output only those credit card numbers which have two or more consecutive occurrences of the same digit (which may be separated by a space, if they are in different segments). Assume that the credit card numbers will have 4 space separated segments with 4 digits each.

If the credit card number is 1434 5678 9101 1234, there are two consecutive instances of 1 (though) as highlighted in box brackets: 1434 5678 910[1] [1]234

Here are some credit card numbers where consecutively repeated digits have been highlighted in box brackets. The last case does not have any repeated digits: 1234 5678 910[1] [1]234 2[9][9][9] 5178 9101 [2][2]34 [9][9][9][9] 5628 920[1] [1]232 8482 3678 9102 1232

grep "\(\d\)\s*\1"

15 Practical Grep Command Examples In Linux / UNIXThe Geek Stuff
Examples using grep
Logo
Logo
Source:
https://regexr.com/