From the course: Bash Patterns and Regular Expressions

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Regex to find credit card numbers

Solution: Regex to find credit card numbers - Bash Tutorial

From the course: Bash Patterns and Regular Expressions

Start my 1-month free trial

Solution: Regex to find credit card numbers

(electronic music) - [Instructor] In this challenge, we created a script that looped through the sales.csv file, and for each Visa card, printed the number on the screen. We used Bash's in process regular expression support. Optionally, we added hyphens to every four characters. For this solution, I created a for loop that iterates through each line of the file. I could have used read array to read the file into an array, but I wanted to keep things simple. Then I used the bash if condition with a regex to look for lines that included Visa. Although it wasn't necessary, I accommodated for both lower and capitalized versions of the word Visa. If the line contained Visa, I then used another if condition with a regex to search for a 16 digit number which is then stored in the bash rematch zero array item. Lastly, I printed this on the screen. For the optional challenge, I typed the output of bash rematch zero to sed and used an ere with four back references to add hyphens to the credit…

Contents