Filenames with spaces in Bash script

  • Thread starter Thread starter Chrisas
  • Start date Start date
AI Thread Summary
The discussion centers on processing files generated by a Windows data recorder, which produce filenames with spaces. The user initially attempts to create a script that processes these files by stripping their extensions and generating multiple output files. The first approach using a for loop fails because it does not handle filenames with spaces correctly. Attempts to use awk to format the filenames also result in syntax errors. The solution involves switching to a while loop with the read command, allowing for proper handling of filenames with spaces. The final script effectively uses variable substitution to separate the filename from its extension, ensuring the processing of each file is executed correctly.
Chrisas
Messages
152
Reaction score
0
I get a bunch of input files from a Windows data recorder. It produces file names with spaces everywhere. I am trying to write a script to process these files. This process involves several steps with multiple output files generated. I want to strip off the extension of the input file and use the name with different extensions for the output files.

Example: I can manually type the file names with ' ' and this works
for FILE in 'file 1' 'file 2' 'file 3'
do
stuff1 < ${FILE}.in > ${FILE}.out1
stuff2 < ${FILE}.in > ${FILE}.out2
done

Example: I try to automatically get filenames
for FILE in `ls -1 *.in | awk -F. '{ print $1; }'`
stuff1 < ${FILE}.in > ${FILE}.out1
stuff2 < ${FILE}.in > ${FILE}.out2
done
This fails because FILE contains file 1 file 2 file 3 and so the loop uses just "file" not "file 1".

Example: I tried to use awk to add ' ' around the file names, something like:
for FILE in `ls -1 *.in | awk -F. '{ print "\'", $1, "\'"; }'`
But no matter what I try I get a syntax failure.

Any ideas?
Thanks.
 
Technology news on Phys.org
Solved: The fix was to use read in a while loop instead of using a for loop. I used variable substition instead of awk to separate the file name from the extension

ls -1 *.in | while read FILE
do
FI=${FILE%.in}
stuff1 < "${FI}.in" > "${FI}.out1"
stuff2 < "${FI}.in" > "${FI}.out2"
done
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top