Transpose File

来源:互联网 发布:单片机红外线开关 编辑:程序博客网 时间:2024/06/02 16:17


Given a text file file.txt, transpose its content.

You may assume that each row has the same number of columns and each field is separated by the' ' character.

For example, if file.txt has the following content:

name agealice 21ryan 30

Output the following:

name alice ryanage 21 30
题意:从file.txt中读取数据,相当于调换行和列,将列变成行
思路:主要用了read -a来读取数据到数组中,以及涉及到数组的操作,如${#array_name[@]}表示数组array_name的长度,数组元素的引用${array_name[i]}表示数组array_name的第i个元素(下标从0开始)
代码如下:
while read -a columns #读取一行到数组columns中do for ((i=0; i < ${#columns[@]}; i++)) #  ${#columns[@]}表示数组长度doa[i]="${a[i]} ${columns[i]}"donedone < file.txtfor ((i=0; i < ${#a[@]};i++))doecho ${a[i]}done

0 0
原创粉丝点击