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?
Asked
Active
Viewed 8,261 times
3
Rui F Ribeiro
- 56,709
- 26
- 150
- 232
helpwithsed
- 41
2 Answers
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
-
https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice?noredirect=1&lq=1 – Jeff Schaller Oct 10 '18 at 12:36
-
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)
francescop21
- 318
seddoesn't require an input file, just pipe the output tosed... – pLumo Oct 10 '18 at 12:22