将行转换为每第n行的列

时间:2013-09-13 20:29:53

标签: linux unix rows multiple-columns

我有一个out.txt,我想读入列而不是行。我想将第1-5行变成一列,将6-10变成一列,等等。有人能指出我正确的方向吗?

diff -r ./playground2/GN_GLENDALE_BILLS_130911_113722.txt ./playground/GN_GLENDALE_BILLS_130911_113722.txt
22c22
< N4*MCDONALDS*KY*40512~^M
---
> N4*LEXINGTON*KY*40512~^M
diff -r ./playground2/GN_GLENDALE_BILLS_130911_113723.txt ./playground/GN_GLENDALE_BILLS_130911_113723.txt
22c22
< N4*MCDONALDS*KY*40512~^M
---
> N4*LEXINGTON*KY*40512~^M
diff -r ./playground2/GN_GLENDALE_BILLS_130911_113725.txt ./playground/GN_GLENDALE_BILLS_130911_113725.txt
22c22
< N4*MCDONALDS*KY*40512~^M
---
> N4*LEXINGTON*KY*40512~^M

1 个答案:

答案 0 :(得分:0)

假设文件的行数可以被5整除(或者要忽略任何剩余行),这应该可以正常工作:

filein=out.txt
fid=15                        #file identifier
nlines=`cat $filein | wc -l`  #count number of lines in file
eval "exec $fid<$filein"
for cnt in $(seq 1 $((nlines/5))); do
   read <&$fid line1
   read <&$fid line2
   read <&$fid line3
   read <&$fid line4
   read <&$fid line5
   echo $line1 $line2 $line3 $line4 $line5
done
eval "exec $fid<&-"

基本上,它只是为文件中行数的1/5做一个for循环,一次读取5行并一次吐出所有行。