All, I’m trying to run a sed command to strip out card numbers from certain files. I was trying to do this in a one-liner and I thought all was going well – but I realized that if my first substitute didn’t match the pattern it continued into the next commands. Is there a way to get it to exit if there is no match?
We have 16-22 length card numbers on our system, so I wrote this with a variable length in mind. My specifications were to preserve the first 6 and last 4 of any 16+ digit number, and axe (asterisk) out anything in the middle.
sed 'h;s/[0-9]\{6\}\([0-9]\{5\}\)\([0-9]*\)[0-9]\{4\}/\1\2/;s/./*/g;x;s/\([0-9]\{6\}\)[0-9]*\([0-9]\{4\}\)/\1\2/;G;s/\n//;s/\([0-9]\{6\}\)\([0-9]\{4\}\)\(.*\)/\1\3\2/'
The problem lies in the fact that if this part of the command:
s/[0-9]\{6\}\([0-9]\{5\}\)\([0-9]*\)[0-9]\{4\}/\1\2/
Finds nothing, the pattern space remains the input. It continues into the next command which then replaces everything with asterisks. What I end up with is the input followed by an equal number of asterisks (if it does not match the “card number qualifications” in my first substitute). It works perfectly if it is what is deemed a possible card number.
Any ideas?
You can use branch commands. I added and commented them in place:
UPDATE due to comments.
So you want to transform
in
Try:
Output: