Linux Bash shell one practice : array if else

本文介绍了一段使用Shell脚本处理文件的实践案例,包括读取文件内容,将第一行拆分为数组,并根据特定条件进行数据拼接,最终输出到新的文件中。通过此实践,读者可以深入理解Shell脚本在文件操作和数据处理方面的强大能力。

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;

转载于:https://www.cnblogs.com/huaxiaoyao/p/4437189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值