Stream Manipulation With grep, sed, and xargs

Regex image

It’s not unusual for a developer or data analyst to have the need to modify files with similar pattern across all board. Scenario: Say you have 100 files in your project that contain the text “ORANGE” and you want to replace them all into “APPLE.” A novice approach would be to manually open each file, look for ORANGE, and replace with APPLE. But we can do better with the following UNIX command:

grep -rl "ORANGE" . | xargs sed -i '' 's/ORANGE/APPLE/g'

Let’s see what’s going on here:

And that’s it. There, we have successfully replaced 100 files with one simple command.

Bonus Scenario:

There are many .tmp files laying around in your project. How to delete them all?

find . -iname "*.tmp" | xargs rm

Super Bonus Scenario: This question comes up often in software developer’s job interview question: “How to extract out phone numbers in many huge files?” My approach would be to use grep with clever regular expressions. How complicated does the pattern matching have to be in order to avoid under-match and over-match? That’d open an interesting conversation between the interviewer and the interviewee.