linux按照学号对文件进行排序,linux使用shell 进行文件数据的读取与排序

题目

shell脚本语言编写一个从键盘输入10名学生(含自己)的姓名、 性别、学号和家庭住址,然后按照学号排序,并将排序后的结果在屏幕上按对齐 的方式打印输出的程序。

代码

读入数据

数据排序(这里用的选择排序)

数据输出

#!/bin/bash

declare -a username

declare -a sex

declare -a usercode

declare -a userpath

i=0

# scan data from keyboard or pipe

# cat ../test | while read line

for line in `cat ../test`;

do

index=`expr $i / 4`

pos=`expr $i % 4`

if [ $pos -eq 0 ]

then

username[$index]=${line}

elif [ $pos -eq 1 ]

then

sex[$index]=${line}

elif [ $pos -eq 2 ]

then

usercode[$index]=${line}

else

userpath[$index]=${line}

fi

# echo $i, ${usercode[$i]}

# echo ${username[$i]}, ${sex[$i]}, ${usercode[$i]}, ${userpath[$i]}

i=$(($i+1))

done

arr=(0 1 2 3 4 5 6 7 8 9)

i=0

j=0

# sort it with ascending

while [ $i -lt 10 ]; do

j=$(($i+1))

while [ $j -lt 10 ] ; do

# echo ${usercode[${arr[$j]}]} , ${usercode[${arr[$i]}]}

if [ ${usercode[${arr[$j]}]} -lt ${usercode[${arr[$i]}]} ]

then

tmp=${arr[$j]}

arr[$j]=${arr[$i]}

arr[$i]=$tmp

fi

j=$(($j+1))

done

i=$(($i+1))

done

# echo ${arr[@]}

i=0

while [ $i -lt 10 ]; do

index=${arr[$i]}

printf "%8s %6s %4s %10s\n" ${usercode[$index]}, ${username[$index]}, ${sex[$index]}, ${userpath[$index]}

i=$(($i+1))

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值