DEV Community


Posted on


Efficiently Finding and Replacing Text in Multiple Files Using ripgrep and sed


Convenient way to search for and replace text in multiple files using the powerful ripgrep and sed tools


rg "some string" | xargs -d "\n" sed -i "s/some string/another string/g"
Enter fullscreen mode Exit fullscreen mode

The command searches for the specified string using ripgrep and pipes the results to xargs, which executes the sed command on each line returned by ripgrep. The sed command then replaces each occurrence of "find-string" with "new string" in the specified files.

Here's the breakdown of the command:

  • rg "find-string": This uses ripgrep to search for the string "find-string" in the current directory and all its subdirectories.
  • |: This pipes the output of the previous command (the list of files containing "find-string") to the next command.
  • xargs -d "\n": This takes the output from the previous command and passes it as arguments to the sed command. The -d option narrows down the delimiter to a newline character, so that each line returned by ripgrep is passed as a separate argument to sed. By default it uses new line characters as well as spaces
  • sed -i "s/find-string/new string/g": This is the sed command, which replaces each occurrence of "find-string" with "new string" in the specified files. The -i option tells sed to edit the files in-place, so the changes are saved directly to the original files. The s command is the substitute command, which performs search and replace operations on the specified files. The g flag at the end tells sed to replace all occurrences of the search string, rather than just the first one.


Top comments (1)

oto profile image

ok,thank you

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.