It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.
The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.
touch sample.txt
find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;
The expected output is:
./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT
Instead it produces:
./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT
(the example has been tested also with old-school command substitution through backquotes '`' with the same result)
What am I doing wrong?
sed -e "s/txt/TXT/g"
to leave alone the secondtxt
?{}
is expanded beforesed
gets to see it. – Satō Katsura Oct 30 '17 at 10:58{}
is expanded byfind
, aftersed
has seen it ("it" being the string{}
). – Kusalananda Oct 30 '17 at 11:39