0

New to coding I have a very simple loop that Im using to rename files, but it stops after the first iteration.

Im running the loop:

for x in `cat list_genomes.txt`
do
        mv -v $x"_busco/run_hypocreales_odb10" "/busco_runs/run_"$x
done

and the error I get is:

‘FV25228_busco/run_hypocreales_odb10’ -> ‘busco_runs/run_FV25228’
mv: cannot stat ‘\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV32968\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV34765\r_busco/run_hypocreales_odb10’: No such file or directory
mv: cannot stat ‘FV-NC-01\r_busco/run_hypocreales_odb10’: No such file or directory

so the first line works, but then it cannot read the rest.

list_genomes.txt looks like:

FV25228 
FV32968 
FV34765 
FV-NC-01 
Fc25332
  • Add sample input from `list_genomes.txt` and expected changes – Gilles Quénot Dec 20 '22 at 19:42
  • 2
    Looks like the file has CRLF EOLs. But that’s not a good way to iterate over lines in a file anyway, see https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash. – Biffen Dec 20 '22 at 19:44
  • 1
    http://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29 – Gilles Quénot Dec 20 '22 at 19:46
  • For help with the CRLF line endings, see ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) and ["How to convert Windows end of line in Unix end of line (CR/LF to LF)"](https://stackoverflow.com/questions/3891076/how-to-convert-windows-end-of-line-in-unix-end-of-line-cr-lf-to-lf) – Gordon Davisson Dec 20 '22 at 20:19

1 Answers1

0

Your input file has originated on a system that is not identical to the one where you are running the script.

The "\r" on the 2nd-5th line indicate that the file came from a Windows-based system. The person who created the file should have chosen a format that is compatible with system on which the data was going to be processed, which in this case appears to be Unix/Linux based.

If the creator of your input file doesn't change the way they do that, you need to massage the file before feeding it to your program logic, using the command dos2unix.

The following would likely provide you with the necessary fix if all else is correct:

#!/bin/bash

dos2unix -n list_genomes.txt list_genomes.fix
while read line
do
        mv -v "${line}_busco/run_hypocreales_odb10" "/busco_runs/run_${line}"
done < list_genomes.fix
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11
  • `dos2unix` doesn't write anything to stdout, so you can't pipe its output. – Benjamin W. Dec 20 '22 at 20:35
  • @Benjamin, I updated per one of the forms allowed, but the stdout is redirected as my original, as per man page for dos2unix. Look for the string "dos2unix > out.txt" . – Eric Marceau Dec 20 '22 at 20:39
  • It prints to stdout if it reads from stdin, like `dos2unix < input.txt`; if you give it a filename without redirection, it updates the file in-place (or copies and renames such that it looks like in-place). – Benjamin W. Dec 20 '22 at 20:45