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 6,814 times
1
Rui F Ribeiro
- 55,929
- 26
- 146
- 227
helpwithsed
- 21
- 1
- 2
-
`sed` doesn't require an input file, just pipe the output to `sed`... – pLumo Oct 10 '18 at 12:22
-
unless they want to have sed operate on the matching files (not on their file *names*) – Jeff Schaller Oct 10 '18 at 12:28
-
1Check out https://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files/112024#112024 – Jeff Schaller Oct 10 '18 at 12:29
-
See also: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice/321753#321753 – Jeff Schaller Oct 10 '18 at 12:30
2 Answers
4
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,231
- 2
- 41
- 66
-
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
-
2
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
- 288
- 5
- 14