I tried to write a code that which rotates each line in a text file. For example, given the next line:
a b c
the output will be:
c b a
this script get as argument only one argument - the name of the text file. In addition, I want to do it so will be signficance to extra spaces. namely, given the next line:
a b c
the output will be:
c b a
comment: the output will be in a new file with same name just with suffix of .rotate.
My code:
#!/bin/bash
name_of_file=$1
first_step=1
while read line; do
length_of_line=`echo -n "${line}" | wc -c`
rotate_line=()
index_of_line=0
index_of_rotate=$(( length_of_line - 1 ))
while (( ${index_of_line} < ${length_of_line} )); do
rotate_line[${index_of_rotate}]="${line[@]:${index_of_line}:1}"
let index_of_line++
let index_of_rotate--
done
if (( ${first_step} == 1 )); then
echo "${rotate_line[@]}" > $1.rotate1
first_step=0
else
echo "${rotate_line[@]}" >> $1.rotate1
fi
done < ${name_of_file}
the problem:
I don't know why, but, given this line:
a b c
the output is:
c b a
from where is the extra space?
COMMENT: While I checked the rotate array letter-by-letter, it's Ok (without extra spaces) - But, while I print it with "${rotate_line[@]}" it's adds a new spaces.. why?
rev file?while IFS= read -r line; do for ((i=${#line};i>0;i--)); do echo -n "${line:$i-1:1}"; done; echo; done < file?>>operator that don't delete the old file. and>operator delete the old file. (If before of the running exists file with this name I want to delete its)