0

This question already has an answer here:

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?

marked as duplicate by don_crissti, eyoung100, jimmij, hildred, Michael Homer Aug 23 '15 at 2:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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.

  • Unix has a tool for everything. +1. – John1024 Aug 23 '15 at 0:19
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.