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
  • 'Sed' Command #1
  • 'Sed' Command #2
  • 'Sed' Command #3
  • 'Sed' Command #4
  • 'Sed' Command #5

Was this helpful?

  1. Linux Shell
  2. Grep Sed Awk

Sed

PreviousGrepNextAwk

Last updated 1 year ago

Was this helpful?

'Sed' Command #1

For each line in a given input file, transform the first occurrence of the word 'the' with 'this'. The search and transformation should be strictly case sensitive.

sed 's/\bthe\b/this/'

'Sed' Command #2

For each line in a given input file, transform all the occurrences of the word 'thy' with 'your'. The search should be case insensitive, i.e. 'thy', 'Thy', 'tHy' etc. should be transformed to 'your'.

sed 's/\bthy\b/your/Ig'

'Sed' Command #3

Given an input file, in each line, highlight all the occurrences of 'thy' by wrapping them up in brace brackets. The search should be case-insensitive.

sed 's/thy/{&}/Ig'

'Sed' Command #4

Given n lines of credit card numbers, mask the first 2 digits of each credit card number with an asterisk (i.e., *) and print the masked card number on a new line. Each credit card number consists of four space-separated groups of four digits. For example, the credit card number 1234 5678 9101 1234 would be masked and printed as **** **** **** 1234.

sed 's/\(\d\{4\}\s\)\{2\}\(\d\{4\}\)/**** **** ****/g'

'Sed' Command #5

Given an input file, with N credit card numbers, each in a new line, your task is to reverse the ordering of segments in each credit card number. Assume that the credit card numbers will have 4 space separated segments with 4 digits each.

If the original credit card number is 1434 5678 9101 1234, transform it to 1234 9101 5678 1434.

sed -E 's/([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/\4 \3 \2 \1/' 
Sed Command in Unix and Linux ExamplesProgramming and Tools Blog -
Sed
Logo
The Grymoire's tutorial on SED
Logo
Logo