Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Sed command #5 problem solution

YASH PAL, 31 July 2024

In this HackerRank Sed command #5 problem solution we have 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.

Useful References: This particular page on StackOverflow has a relevant example about sed, groups and backreferences. Here’s a detailed tutorial covering groups and backreferences.

Input Format

N credit card numbers, each in a new line, credit card numbers will have 4 space-separated segments with 4 digits each.

Constraints

1 <= N <= 20

However, the value of N does not matter while writing your command.

Output Format

N lines, each containing a credit card number with the ordering of its segments reversed.

HackerRank Sed command #5 problem solution

Problem solution.

sed -E 's/([0-9]{4}) ([0-9]{4}) ([0-9]{4}) ([0-9]{4})/4 3 2 1/'

Second solution.

cat /dev/stdin | sed -r 's/([0-9]{4}) ([0-9]{4}) ([0-9]{4}) ([0-9]{4})/4 3 2 1/g'

Third solution.

sed 's/([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)/4 3 2 1/'

Fourth solution.

cat - > input
sed 's/([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)/4 3 2 1/' input

coding problems solutions linux shell

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes