Split text file into blocks and save

Viewed 138

I have a big text file saved in the name test.txt.Now i want to split the big text files into blocks at ... symbol and want to save in the name same as what is there after /home/niu/ . (In the data example below i need the blocks of data to be saved in 20190630_073410_1.5_29_PCK.txt for the first block, 20180630_073410_1.5_29_PCK.txt for second block and 20190830_093410_1.5_29_PCK.txt for third block.

Thus i tried the code below:

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

It does not work: i hope somebody will help me.Thanks.

My data saved in test.txt is given below:

    ...........................................................................................................   
    /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
3 Answers
awk '/\.\.\./{close(out); next} {split($1, a, "/"); out=a[4]; print > out}' file

You can use this awk. I have assumed that dots (...) exist only in the separating lines, also all the other lines starts with /home/niu/filename.txt, from where we get the output filename. If this is not the case, please update the question.

Could you please try following, written and tested with shown samples in GNU awk.

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

Explanation: Adding detailed explanation for above.

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.

EDIT: As per OP there could be lines starting with spaces so in that case try.

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

you can use csplit like this:

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