0

Its about 2500 CSV files who need to be replaced with and comma values and i need a script to select a range.

R000001.CSV....R000499 in 5 directories but filename needs to be changed with the for next loop.

I need some advice to make this script works.

BTW _1 its a mark for the processed files.

read -p "First Value: " f
read -p "Last Value: " l
read -p "Secuence $f to $l ... is correct press <enter> to continue or Ctrl+C to cancel"
for a in {$f..$l..1}
  do
     tr -d '\r' ';'< R*$f.CSV > R*$f_1.CSV

 done
1
  • "who need to be replaced with and comma values" Please clarify. Commented Nov 10, 2015 at 3:21

1 Answer 1

1

Try:

for a in $(seq -f '%06.0f' "$f" "$l")
do
    echo tr -d '\r' ';' <R*$a.CSV >R*${a}_1.CSV
done

Notes:

  1. The expression {$f..$l..1} will not work because variables cannot be put inside of a brace expansion.

  2. The utility seq can be used in place of brace expansion. With the -f option, we can select the format that matches your file names.

  3. Inside the loop, you want to reference $a rather than $f.

  4. The expression $f_1 will not work as you expect. Both _ and 1 are legal characters in shell variable names. Consequently, the shell will try to find a shell variable named f_1 and expand it. That is not what you want. Using braces around a shell variable name, such as ${f}_1, avoids this.

Without seq

for ((i=$f; i<=$l; i++)) do j=$(printf '%06i' $i); tr '\r' ';'<"R$j.CSV" >"R${j}_1.CSV"; done

Or, written over multiple lines for clarity:

for ((i=$f; i<=$l; i++))
do
    j=$(printf '%06i' $i)
    tr '\r' ';'<"R$j.CSV" >"R${j}_1.CSV"
done
Sign up to request clarification or add additional context in comments.

3 Comments

thanks John but seq command does not exists in osx snow leopard so i research more and now i got this.. for ((i=$f; i<=$l; i++)) do tr '\r' ';'< R*$i.CSV > R*${i}_1.CSV done
@Ralcala Yes, those * could cause problems. See the updated answer for a variation on your code that should work (it works for me). Separately, for whatever its worth, I checked the man pages that Apple puts online and it appears that, in the future, you will be able to use seq: Apple includes seq in the current OSX version.
Thank you very much for your time John and your instructions. The script works thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.