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.BBBextension to create the new file name, which I callnew_file,and to top it off,I pass this modified file tocommandBwith the-ioption!or you can use process substitution, so I use
sedto change the file extension from.AAAto.BBBin the file name,and then, I pass this fancy modified file name tocommandBusing something called process substitution, which treats the command's output like a temporary file :