I have a for loop that accepts a file with one type of extension, but the final command in the pipe requires the STDIN file to have an extension of another type. In my pipe I use awk
to change the file type so that it is properly formatted for the final command, but the extension is still linked to the initial input file.
For example:
for file in *.AAA
do
commandA $file | awk ' { print } ' | commandB
A typical use of commandB:
commandB -i myfile.BBB
Is there a way for me to change the extension of a file in the middle of a for loop & pipe?
I think you can change the extension of a file in the middle of a for loop and pipe, As you see ${file%.*}
is a cool trick that basically removes the file extension (*.AAA
) from the original file name, and then, I add the .BBB
extension to create the new file name, which I call new_file
,and to top it off,I pass this modified file to commandB
with the -i
option!
for file in *.AAA
do
new_file="${file%.*}.BBB" #change the extension to .BBB
commandA "$file" | awk '{ print }' | commandB -i "$new_file"
or you can use process substitution, so I use sed
to change the file extension from .AAA
to .BBB
in the file name,and then, I pass this fancy modified file name to commandB
using something called process substitution, which treats the command's output like a temporary file :
for file in *.AAA; do
commandA "$file" | awk '{ print }' | commandB -i <(commandA "$file" | awk '{ print }' | sed 's/\.AAA$/.BBB/')
done