2

I am using a simple for loop to process certain input files and get the output in one file. What I am using is

for k in ../some_directory/*.txt; 
    do command (containing -i $k -o outputfile.txt); 
done

Now each loop gives an output. I want the output to be written in one file. This loop just replaces all the previous files and gives me a number of outputfile.txt files each with the output from every loop. How can I append the command to one file? I don't want the screen output but the output from each of the command.

NOTE: I am not talking about this or this

  • 1
    You can try &>> or >> nstread of -o to get the output. the only difference on those link os > this will overwrite the file again and again if you use >> twice it will append the output. – Mongrel Jun 15 '16 at 3:56
  • another solution to redirect the output of for...done to a single file. The command should be capable to direct its output to stdout by default or by supplying a dash (-) to -o option (the widely used convention) – Serge Jun 15 '16 at 4:20
2

You have two options here to get your desired result:

for k in /path/to/*.txt; do
    some_command -i "$k" >> /path/to/output.txt
done

OR

for k in /path/to/*.txt; do
    some_command -i "$k"
done >> /path/to/output.txt

If your program doesn't write to standard output, and only writes to a file specified with -o, you can do this:

for k in /path/to/*.txt; do
    some_command -i "$k" -o /tmp/output.txt
    cat /tmp/output.txt >> /path/to/real_output.txt
done
rm /tmp/output.txt
  • Sorry this does not work. Maybe my question is not clear enough. So I have a file. I give this as an input file to the command in the loop. If I don't specify any -o file, the output gets appended to this input file. Now I want to loop it so that in every loop the output gets appended to this same input file. – Vishal Minhas Jun 15 '16 at 8:21
  • I have updated my proposed answer to reflect this. – DopeGhoti Jun 15 '16 at 16:30
  • 2
    The parens are not needed in the second for loop. Doing for ...; do ... done >>outfile works just fine. – Kusalananda Jun 16 '16 at 6:47
0

Just thought of a simpler solution. I just needed to give the same file name as output as the input. That solved my problem as all the changes get appended and the old output file gets backed up.

Thanks

0
for name in ../some_directory/*.txt; do
    outfile="$name"-out
    command (containing -i "$name" -o "$outfile"); 
done

This would construct the name of the output file from the name of the input file by simply appending the string -out to it.

Note that outfile=out-"$name" would not work as $name also contains the path to the filename.

Would you want to prepend a string to the filename, you cold do

outdir=${name%/*}        # remove filename component from $name
outfile=out-${name##*/}  # remove the path component from $name (and prepend string)

Then use $outdir/$outfile as the output file path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.