如何遍历包含输入文件的文件夹,并将输出文件写入不同的文件夹?

huangapple go评论85阅读模式
英文:

How can I loop through a folder containing inputfiles and write outputfiles in a different folder?

问题

我有一个bash脚本,它应该循环遍历一个目录(source-folder),并运行一个程序(myProgram)来处理目录中的所有文件,并将输出写入另一个目录。输出文件的名称应该略有不同。

我的myProgram需要5个参数:

  1. ./myProgram PAR1 infile1.txt PAR3 PAR4 infile_run1.txt

source-folder:

  • infile1.txt
  • infile2.txt
  • infile3.txt

dest-folder

  • infile_run1.txt
  • infile_run2.txt
  • infile_run3.txt

我可以循环遍历文件夹,使用所有文件运行我的程序,但我的问题是如何将输出文件写入不同的目录:使用下面的代码只会创建一个名为NewName的文件。

如何为每个输入文件(来自source-folder)获得一个输出文件(到dest-folder)?

  1. #!/bin/bash
  2. for filename in ~/path/to/source-folder/*;
  3. do
  4. echo $filename
  5. ./myProgram -A $filename -B -C ~/path/to/dest-folder/Newname
  6. done
英文:

I have a bash script that is supposed to loop through a directory (source-folder) and run a program (myProgram) with all files in the directory and write the output to another directory. The files in output are supposed to have a slightly different name.

My myProgram takes 5 parameters:

  1. ./myProgram PAR1 infile1.txt PAR3 PAR4 infile_run1.txt

source-folder:

  • infile1.txt
  • infile2.txt
  • infile3.txt

dest-folder

  • infile_run1.txt

  • infile_run2.txt

  • infile_run3.txt

I can loop through the folder, run my program with all files but my problem is writing the output files into a different directory: Using my code below creates just 1 File named NewName.

How do I get an output file (to the dest-folder) for each input file (from the source-folder)?

  1. #!/bin/bash
  2. for filename in ~/path/to/source-folder/*;
  3. do
  4. echo $filename
  5. ./myProgram -A $filename -B -C ~/path/to/dest-folder/Newname
  6. done

答案1

得分: 1

我对myProgram的功能和选项不确定,所以只是作为一个展示,我创建了一个名为myProgram.sh的文件。它的目的是计算每个文件中的字符数,并将计数记录在输出文件中:

  1. #!/bin/bash
  2. src="./source-folder"
  3. dest="./dest-folder"
  4. for input in "$src"/infile*.txt;
  5. do
  6. echo "Processing: $input"
  7. # 确定基本名称
  8. base=$(basename "$input" .txt)
  9. # 构造输出文件名
  10. output="$dest/${base}_run1.txt"
  11. # 调用myProgram来计算输入文件中的字符数并捕获输出
  12. char_count=$(./myProgram.sh "$input")
  13. # 将字符计数写入输出文件
  14. echo "Character count: $char_count" > "$output"
  15. done

myProgram.sh的内容如下:

  1. #!/bin/bash
  2. # 计算输入文件中的字符数
  3. char_count=$(wc -c < "$1")
  4. echo "$char_count"
英文:

I was unsure about the features and options of myProgram, so just as a showcase, I created a file called myProgram.sh. Its purpose is to calculate the number of characters in each file and record the counts in the output files:

  1. #!/bin/bash
  2. src=&quot;./source-folder&quot;
  3. dest=&quot;./dest-folder&quot;
  4. for input in &quot;$src&quot;/infile*.txt;
  5. do
  6. echo &quot;Processing: $input&quot;
  7. # Determine the base name
  8. base=$(basename &quot;$input&quot; .txt)
  9. # Construct the output filename
  10. output=&quot;$dest/${base}_run1.txt&quot;
  11. # Call myProgram to count characters in the input file and capture the output
  12. char_count=$(./myProgram.sh &quot;$input&quot;)
  13. # Write the character count to the output file
  14. echo &quot;Character count: $char_count&quot; &gt; &quot;$output&quot;
  15. done

And the myProgram.sh:

  1. #!/bin/bash
  2. # Count the number of characters in the input file
  3. char_count=$(wc -c &lt; &quot;$1&quot;)
  4. echo &quot;$char_count&quot;

答案2

得分: 1

以下是翻译好的内容:

  1. 对于~/path/to/source-folder/目录下的每个文件,执行以下操作:
  2. ./myProgram -A $filename -B -C ~/path/to/dest-folder/$(basename $filename)

这可能是我会这样做的方式,尽管我没有测试过,但它应该能给你一个开始的方向。

英文:
  1. for filename in ~/path/to/source-folder/*; do
  2. ./myProgram -A $filename -B -C ~/path/to/dest-folder/$(basename $filename)
  3. done

is probably how i would do it though I did not test it, but it should give you something to start on

huangapple
  • 本文由 发表于 2023年8月8日 21:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定