Search and Replace in Files
Linux, Perl, Regular Expressions (regex) No Comments »Here is how to do a search and replace using Perl regex over a set of files:
Here is how to do a search and replace using Perl regex over a set of files:
Another task which hits my desk often is getting “data” out of text files. Such as names, email addresses, phone numbers, ID numbers, etc. Here is a quick way to parse or rip out “data” from a text file.
I suggest using ‘grep’ — grep is one of those applications which is more powerful than the casual user realizes. And let’s face it, if you use or know what grep is you probably do not consider yourself a casual user.
grep -o -P "[regex_pattern]" [filename]
The -o tells grep to only output what it matched in your pattern where your [regex_pattern] is your pattern.
The -P tells grep to use Perl Regular Expressions
Parsing out email addresses is a tedious task. This task hits my desk on a consistent basis. Usually, in my case, the list is coming out of Outlook. If the target list is coming out of Outlook make sure you export using Tab Separated for DOS (thus, click on File–>Import/Export–>Export to a File–>Tab Separated (DOS)–>[select_the_target_folder]). Take this target file and execute this command:
cat [filename] | grep -o -P "[[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(local|aero|coop|info|museum|name|([0-9]{ 1,3})|([a-zA-Z]{2,3}))]*"
Alternatively I usually need a list of unique addresses:
cat [filename] | grep -o -P "[[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(local|aero|coop|info|museum|name|([0-9]{ 1,3})|([a-zA-Z]{2,3}))]*" | sort -u
Recent Comments