【问题标题】:turn rows into column for every nth row每第 n 行将行转换为列
【发布时间】:2013-09-13 20:29:52
【问题描述】:

我有一个 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

【问题讨论】:

    标签: linux unix rows multiple-columns


    【解决方案1】:

    假设文件有许多行可以被 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 行并一次将它们全部吐出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2017-05-20
      • 1970-01-01
      • 2023-01-31
      • 2012-03-08
      • 2015-06-13
      相关资源
      最近更新 更多