Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

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 linux shell

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes