码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Bash shell one practice : array if else

时间:2015-04-18 13:05:41      阅读:144      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!