加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux使用shell 进行文件数据的读取与排序

发布时间:2020-12-14 00:44:45 所属栏目:Linux 来源:网络整理
导读:#!/bin/bashdeclare -a usernamedeclare -a sexdeclare -a usercodedeclare -a userpathi=0# scan data from keyboard or pipe# cat ../test | while read linefor line in `cat ../test`;do index=`expr $i / 4` pos=`expr $i % 4` if [ $pos -eq 0 ] then
#!/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 %10sn" ${usercode[$index]},${username[$index]},${sex[$index]},${userpath[$index]}
    i=$(($i+1))
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读