Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将文本文件拆分为块并保存_Linux_Bash_For Loop_Awk_Split - Fatal编程技术网

Linux 将文本文件拆分为块并保存

Linux 将文本文件拆分为块并保存,linux,bash,for-loop,awk,split,Linux,Bash,For Loop,Awk,Split,我在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

我在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

请您尝试使用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

您可以使用此awk。我假设点(
)仅存在于分隔行中,并且所有其他行都以
/home/niu/filename.txt开头,从中我们可以获得输出文件名。如果不是这样,请更新问题。

您可以像这样使用csplit:

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

文件中的行是否按第一列排序(即,按目录/文件名排序)?文件中是否确实有
..
行?是否所有行都以目录/文件名开头,且格式相同
/*.*.txt
?给定输入的示例集,请使用所需的输出更新您的问题(例如,3个新文件?并显示每个文件的内容)
csplit test.txt '/^\./' {*}