0

I have a folder called files that has 100 files, each one has one value inside;such as: 0.974323

This my code to generate those files and store the single value inside:

DIR="/home/XX/folder"
INPUT_DIR="/home/XX/folder/eval"
OUTPUT_DIR="/home/XX/folder/files"

for i in $INPUT_DIR/*
do
groovy $DIR/calculate.groovy $i > $OUTPUT_DIR/${i##*/}_rates.txt
done

That will generate 100 files inside /home/XX/folder/files, but what I want is one single file that has in each line two columns separated by tab contain the score and the name of the file (which is i).

the score \t name of the file

So, the output will be:

0.9363728 \t resultFile.txt
0.37229 \t outFile.txt

And so on, any help with that please?

LamaMo
  • 576
  • 2
  • 8
  • 19

1 Answers1

1

Assuming your Groovy program outputs just the score, try something like

#!/bin/sh
# ^ use a valid shebang
# Don't use uppercase for variables
dir="/home/XX/folder"
input_dir="/home/XX/folder/eval"
output_dir="/home/XX/folder/files"

# Always use double quotes around file names
for i in "$input_dir"/*
do
  groovy "$dir/calculate.groovy" "$i" |
  sed "s%^%$i\t%"
done >"$output_dir"/tabbed_file.txt

The sed script assumes that the file names do not contain percent signs, and that your sed recognizes \t as a tab (some variants will think it's just a regular t with a gratuitous backslash; replace it with a literal tab, or try ctrl-v tab to enter a literal tab at the prompt in many shells).

A much better fix is probably to change your Groovy program so that it accepts an arbitrary number of files as command-line arguments, and includes the file name in the output (perhaps as an option).

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Regarding uppercase vs lowercase for variables, see https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – tripleee Nov 15 '18 at 08:42