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

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

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

问题

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

我的myProgram需要5个参数:

 ./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)?

#!/bin/bash
for filename in  ~/path/to/source-folder/*;    
do    
	echo  $filename      
	./myProgram -A $filename -B -C ~/path/to/dest-folder/Newname       
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:

 ./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)?

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

答案1

得分: 1

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

#!/bin/bash

src="./source-folder"
dest="./dest-folder"

for input in "$src"/infile*.txt;
do
    echo "Processing: $input"
    
    # 确定基本名称
    base=$(basename "$input" .txt)
    
    # 构造输出文件名
    output="$dest/${base}_run1.txt"
    
    # 调用myProgram来计算输入文件中的字符数并捕获输出
    char_count=$(./myProgram.sh "$input")
    
    # 将字符计数写入输出文件
    echo "Character count: $char_count" > "$output"
done

myProgram.sh的内容如下:

#!/bin/bash

# 计算输入文件中的字符数
char_count=$(wc -c < "$1")
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:

#!/bin/bash

src=&quot;./source-folder&quot;
dest=&quot;./dest-folder&quot;

for input in &quot;$src&quot;/infile*.txt;
do
    echo &quot;Processing: $input&quot;
    
    # Determine the base name
    base=$(basename &quot;$input&quot; .txt)
    
    # Construct the output filename
    output=&quot;$dest/${base}_run1.txt&quot;
    
    # Call myProgram to count characters in the input file and capture the output
    char_count=$(./myProgram.sh &quot;$input&quot;)
    
    # Write the character count to the output file
    echo &quot;Character count: $char_count&quot; &gt; &quot;$output&quot;
done

And the myProgram.sh:

#!/bin/bash

# Count the number of characters in the input file
char_count=$(wc -c &lt; &quot;$1&quot;)
echo &quot;$char_count&quot;

答案2

得分: 1

以下是翻译好的内容:

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

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

英文:
for filename in  ~/path/to/source-folder/*; do
    ./myProgram -A $filename -B -C ~/path/to/dest-folder/$(basename $filename)
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:

确定