0

I need to breakup a file into two separate files based on a particular line for example:

.
.
sc_outfifo 0.000E+00 0.000E+00 0.000E+00 0.000E+00 1.586E-02 1.586E-02 1.586E-02
Fub_Level_DACGE(%) PS0_SI.0 PS0_SI.0 PS0_SI.0 PS1_SI.0 PS1_SI.0 
siunit 98.91 98.81 98.91 90.06 96.37 81.37 81.37 16655
.
.

So all the lines above Fub_Level_DACGE(%) goes in one file and the rest including Fub_Level_DACGE(%) goes in another file. Any idea how it can be done?

steeldriver
  • 81,074
Souvik
  • 21

2 Answers2

3

The utility that's actually designed for this is csplit

NAME
       csplit - split a file into sections determined by context lines

SYNOPSIS
       csplit [OPTION]... FILE PATTERN...

So in this case

csplit file /Fub_Level_DACGE\(%\)/

will produce files xx00 and xx01 containing the two parts.

steeldriver
  • 81,074
1

This will read file file0 and write its lines to file1 until the line starting Fub_Level_DACGE is reached. Starting with that line, all lines are written to file2:

awk -v f=file1 '/^Fub_Level_DACGE/{f="file2"} {print>f}' file0

How it works

  • -v f=file1

    Set the variable f to the name of the initial output file, file1.

  • /^Fub_Level_DACGE/{f="file2"}

    When we reach the line which starts Fub_Level_DACGE, assign to f the name of the second output file, file2.

  • print>f

    Write the current line to the file whose name is in f.

John1024
  • 74,655