2

I'm sorry if this has been answered but since I'm not positive what exactly is the problem (among several possibilities) I haven't been successful in my searches.

What I want to do is take label numbers that are each written as a line in a text file, do things with files containting that label, and output the results to a file. What I have is this:

cat good_PFC.txt | while read line;
do  
 base_file=${line}_blah.nii.gz
 new_fa=${line}_fa_uncmasked.nii.gz
 new_tr=${line}_tr_uncmasked.nii.gz 

 if [ -e $base_file ]; then

  echo -n "$line " >> FA_unc_stats.txt
  fslstats $new_fa -M | tr '\n' ' ' >> FA_unc_stats.txt
  fslstats $new_fa -S | tr '\n' ' ' >> FA_unc_stats.txt     

 else
  echo $line "not a file"
 fi;
done

In which fslstats is a command that outputs numbers and good_PFC.txt is a test file containing

123
125
132

The output in FA_unc_stats.txt is

123 0.221061  0.097268

What's wrong is, the terminal correctly outputs "125 not a file", but does nothing with 132, which I know happens to point to a real file. So I believe something is wrong with the syntax in my while loop, but I don't know what! I bet it's something stupid but I just can't figure it out. Thanks!

ETA: Fixed by adding a newline to the end of good_PFC.txt Now the problem is I need a newline written to the output file whenever I get to a new label, but it doesn't do that. I tried adding

echo /n >> FA_unc_stats.txt

first, but it prints "/n" on it's own line... I fail at newline commands!

0

4 Answers 4

1

Do you know if the loop is being run on the last line at all? Bash may be skipping the last line due to a lack of a newline terminator. Try adding a newline to the last line in the file and see if that fixes the problem.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh this was it! Who new I needed a newline at the end of the file (well obvs. you did). Now I have a new problem writing the output... amended above.
Ok, can you amend your question and paste the output you're now getting?
Sorry, it just took longer than I thought to amend the question. Done now.
Ok, seems like the remaining problem is that you're having trouble trying to make echo print a newline. You can do just echo with no parameters to print a newline. echo -n "\n" will do it too but that's probably a little silly. I think your problem is that you were using /n for newline, but it's actually \n
Oh man, I've been at this too long, making silly mistakes. I finally figured it out by simply changing " fslstats $new_fa -S | tr '\n' ' ' >> FA_unc_stats.txt " to " fslstats $new_fa -S >> FA_unc_stats.txt " No need to print a \n at all if I let it come out naturally at the end! Thanks again for the help.
1

Just add 'echo $line' and you will see if the read loop is working as you expect.

Comments

0

Remove the first pipe with

while read line; do
  ...
done <good_PFC.txt

Comments

-1

try putting the fslstsats commands in back ticks:

`fslstats $new_fa -M | tr '\n' ' ' >> FA_unc_stats.txt`
 `fslstats $new_fa -S | tr '\n' ' ' >> FA_unc_stats.txt

`

1 Comment

-1: that will attempt to execute the results of the command. The fact that the results are empty (due to the redirection) is irrelevant.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.