SHELL11. 转置文件的内容
描述
写一个 bash脚本来转置文本文件nowcoder.txt中的文件内容。Bash 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2021-06-24
#/bin/bash lines=() while read line do lines[${#lines[@]}]=$line done<nowcoder.txt rows=${#lines[@]} t=(${lines[0]}) columns=${#t[@]} for ((i=0;i<$columns;i++)) do for ((j=0;j<$rows;j++)) do tmp=(${lines[$j]}) echo "${tmp[$i]} " done done
Bash 解法, 执行用时: 2ms, 内存消耗: 420KB, 提交时间: 2021-12-24
declare -A map while read -a arr do for i in ${!arr[@]} do map[$i]="${map[$i]} ${arr[$i]}" done done < nowcoder.txt for i in ${!map[@]} do echo ${map[$i]} done
Bash 解法, 执行用时: 2ms, 内存消耗: 420KB, 提交时间: 2021-11-29
declare -a arr1 declare -a arr2 while read line do arr=($line) arr1[${#arr1[@]}]=${arr[0]} arr2[${#arr2[@]}]=${arr[1]} done < nowcoder.txt echo ${arr1[@]} echo ${arr2[@]}
Bash 解法, 执行用时: 2ms, 内存消耗: 428KB, 提交时间: 2021-09-28
#!/bin/bash lines=() while read line do lines[${#lines[@]}]=$line done<nowcoder.txt rows=${#lines[@]} t=(${lines[0]}) columns=${#t[@]} for ((i=0;i<$columns;i++)) do for ((j=0;j<$rows;j++)) do tmp=(${lines[$j]}) echo "${tmp[$i]} " done done
Bash 解法, 执行用时: 2ms, 内存消耗: 504KB, 提交时间: 2021-07-12
#!/bin/bash declare -a tmp1 declare -a tmp2 i=0 j=0 while read line do arr=($line) tmp1[i]=${arr[0]} tmp2[j]=${arr[1]} let i++ let j++ done for k in ${tmp1[@]} do echo -en "${k} " done for ((l=0;l<${#tmp2[*]};l++)) do echo -en "${tmp2[l]} " done