首页 > 系统服务 > 详细

Linux Bash shell one practice : array if else

时间:2015-04-18 13:04:02      阅读:257      评论:0      收藏:0      [点我收藏+]

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;

Linux Bash shell one practice : array if else

原文:http://www.cnblogs.com/huaxiaoyao/p/4437189.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!