【问题标题】:Split text file into blocks and save将文本文件拆分成块并保存
【发布时间】:2021-01-12 01:51:07
【问题描述】:

我有一个以名称 test.txt 保存的大文本文件。现在我想将大文本文件拆分为 ... 符号处的块,并希望以与 /home/niu/ 之后的名称相同的名称保存。 (在下面的数据示例中,我需要将数据块保存在20190630_073410_1.5_29_PCK.txt 中用于第一个块,20180630_073410_1.5_29_PCK.txt 用于第二块,20190830_093410_1.5_29_PCK.txt 用于第三块。

因此我尝试了以下代码:

#!/bin/sh
for file in 'test.txt'
do
split -l '...'
done

它不起作用:我希望有人能帮助我。谢谢。

我保存在test.txt中的数据如下:

    ...........................................................................................................   
    /home/niu/20190630_073410_1.5_29_PCK.txt 470.2359935984357 41573823894247.63 53.46648291467124 216 1 0.1
    /home/niu/20190630_073410_1.5_29_PCK.txt 13.124782961287574 219608788311302.7 53.46425102814092 219 1 0.6
    /home/niu/20190630_073410_1.5_29_PCK.txt 4.092419925137149 12174862157739.746 53.44206693334351 291 1 1.1
    ...........................................................................................................
    /home/niu/20180630_073410_1.5_29_PCK.txt 2.241494955966288 363350265475740.4 53.36874778729164 219 1 0.1
    /home/niu/20180630_073410_1.5_29_PCK.txt 1.6671382966847936 282579486756.3921 53.234249504389624 218 1 2.1
    /home/niu/20180630_073410_1.5_29_PCK.txt 1.4410832347641427 17729080367.579777 53.06935945567802 216 1 2.6
    ...........................................................................................................
    /home/niu/20190830_093410_1.5_29_PCK.txt 1.2367527642969733 5141.577700615736 52.776493933960644 127 0 3.6
    /home/niu/20190830_093410_1.5_29_PCK.txt 1.171644866817557 3279.978138771641 52.65760209064783 135 0 4.1
    /home/niu/20190830_093410_1.5_29_PCK.txt 1.120249969361367 2441.45977994814 52.54882982584634 105 0 4.6

【问题讨论】:

  • 文件中的行是否按第一列排序(即按目录/文件名)?您实际上在文件中有................. 行吗?是否所有行都以目录/文件名和相同格式<directory>/*.*.txt 开头?给定输入样本集,请使用所需的输出更新您的问题(例如,3x 新文件?并显示每个文件的内容)

标签: linux bash for-loop awk split


【解决方案1】:
awk '/\.\.\./{close(out); next} {split($1, a, "/"); out=a[4]; print > out}' file

你可以使用这个 awk。我假设点 (...) 仅存在于分隔行中,所有其他行也以 /home/niu/filename.txt 开头,我们从中获取输出文件名。如果不是这种情况,请更新问题。

【讨论】:

    【解决方案2】:

    你可以像这样使用 csplit:

    csplit test.txt '/^\./' {*}
    

    【讨论】:

      【解决方案3】:

      您能否尝试在 GNU awk 中使用所示示例进行跟踪、编写和测试。

      awk -F'[ /]' '
      !NF || /^\.+/{
        next
      }
      out_file!=$4{
        close(out_file)
        out_file=$4
      }
      {
        print >> (out_file)
      }' Input_file
      

      说明:为上述添加详细说明。

      awk -F'[ /]' '             ##Starting awk program from here and setting space and / for all lines.
      !NF || /^\.+/{             ##Checking condition if number of fields is NULL OR line starting from dot then do following.
        next                     ##next will skip all further statements from here.
      }
      out_file!=$4{              ##Checking condition if prev is NOT equal to out_file then do following.
        close(out_file)          ##Closing file in back end to avoid too many files opened error here.
        out_file=$4              ##Setting out_file as 4th field here.
      }
      {
        print >> (out_file)      ##Printing current line to out_file output file.
      }' Input_file              ##Mentioning Input_file name here.
      

      编辑:根据 OP,可能会有以空格开头的行,所以在这种情况下尝试。

      awk -F'/' '
      !NF || /^\./{
        next
      }
      {
        split($4,arr," ")
      }
      out_file!=arr[1]{
        close(out_file)
        out_file=arr[1]
      }
      {
        print >> (out_file)
      }' Input_file
      

      【讨论】:

        猜你喜欢
        • 2018-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        相关资源
        最近更新 更多