Linux Bash shell one practice : array if else

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;

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。