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.
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