Linux Bash shell one practice : array if else

时间:2021-08-01 08:31:35

shell practice 1
1.require
A B C D
1 2 3 4
5 6 7 8
3 5 8 0
1 2 4 3
after handling:
T A B C D
A 1 2 3 4
B 5 6 7 8
C 3 5 8 0
D 1 2 4 3

1.first need read the first line as an array.
then append the element in the proper position for file;

 

oldifs=$IFS
IFS=$'\n';
counter=0;
cols=''
for line in $(cat test.txt);
do
if [ $counter -eq 0 ]; then
IFS=$' '
read -a cols <<< "$line";
echo 'T ' $line > test_new.txt;
IFS=$'\n';
else
echo "${cols[counter-1]} " $line>>test_new.txt;
fi
let counter++;
done;
cat test_new.txt;
IFS=$oldifs;