From the course: Bash Patterns and Regular Expressions
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: Regex to find credit card numbers - Bash Tutorial
From the course: Bash Patterns and Regular Expressions
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.