thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

β˜… The ack + xargs + sed pattern

β€’ 254 words β€’ 2 min β€’ updated

⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.

I employ this pattern almost every day:

  • use ack to search for a given string in a codebase (git repo)
  • use xargs to iterate through each finding
  • use sed to make a text transformation

Example of the day:

shell
% ack "limit: 2" -l | xargs -n 1 gsed -i -e 's/limit: 2/limit: 5/g'

Given multiple ArgoCD app manifests in the form:

yaml
spec:
  syncPolicy:
    retry:
      limit: 2
      backoff:
        duration: 5s
        factor: 2
        maxDuration: 1m

Replace limit: 2 with limit: 5 in all files. That’s what the command above does:

  • ack -l lists all files that match the given pattern
  • pipe to xargs -n 1 iterates on each of them
  • gsed -i (GNU sed) 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:

shell
% fd -e yaml | xargs ack "limit: 2" -l

In 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:

jq is like sed for JSON data β€” you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.