3

I am trying to run sed on the output from find | grep \.ext$
However, sed requires input file and output file.
How can I save the output line to a variable, or pass it to sed?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

6

This is not true, sed does not require an input file.

Sed is a stream editor [...] used to perform basic text transformations on an input stream (a file or input from a pipeline). (via)

But in your case you want to manipulate files, and not streams, so yes, you need it.


You don't need grep here, you can use find -name "*.ext". If you then want to run sed on all files that have been found, use find -exec to execute a command on any file that has been found.

find -name "*.ext" -exec sed -i '...' {} \;

If for any reason you want to use filenames coming from grep (e.g. your input is not from find), you can use xargs:

grep \.ext$ list_of_files | xargs -I{} sed -i '...' {}
pLumo
  • 22,565
3

You can use sed with multiple input files:

sed 'what/you/wanna/do' file1 file2 file3
sed 'what/you/wanna/do' file*
sed 'what/you/wanna/do' *.txt
sed 'what/you/wanna/do' $(find | grep \.ext)

To save the changes, instead of printing them on stdout, use the option -i (inplace):

sed -i 'what/you/wanna/do' $(find | grep \.ext)