β The ack + xargs + sed pattern
β’ 254 words β’ 2 min β’ updated
I employ this pattern almost every day:
- use
ackto search for a given string in a codebase (git repo) - use
xargsto iterate through each finding - use
sedto make a text transformation
Example of the day:
% ack "limit: 2" -l | xargs -n 1 gsed -i -e 's/limit: 2/limit: 5/g'Given multiple ArgoCD app manifests in the form:
spec:
syncPolicy:
retry:
limit: 2
backoff:
duration: 5s
factor: 2
maxDuration: 1mReplace limit: 2 with limit: 5 in all files. That’s what the command above
does:
ack -llists all files that match the given pattern- pipe to
xargs -n 1iterates on each of them gsed -i(GNUsed) makes a text transformation in-place
It’s not bulletproof, but it works most of the time.
Sometimes I use fd to narrow the search down
to a file pattern. For example:
% fd -e yaml | xargs ack "limit: 2" -lIn this case, though, ack --yaml would be even simpler.
When sed isn’t up for the job, awk tends to be a decent alternative.
Regular expression replacements aren’t always precise.
False positives can easily take place.
Adding \b (word boundaries) sometimes helps.
Other times it’s better to use a syntax-aware tool instead of sed.
For YAML, there’s yq.
For JSON, there’s jq:
jqis likesedfor JSON data β you can use it to slice and filter and map and transform structured data with the same ease thatsed,awk,grepand friends let you play with text.
Backlinks
- fd with xargs: filenames with spaces (Aug 08, 2025)