How to split a large file into two parts, at a pattern?
Given an example file.txt
:
ABC
EFG
XYZ
HIJ
KNL
I want to split this file at XYZ
such that file1
contains lines up-to XYZ
and rest of the lines in file2
.
awksedsplittext processing
How to split a large file into two parts, at a pattern?
Given an example file.txt
:
ABC
EFG
XYZ
HIJ
KNL
I want to split this file at XYZ
such that file1
contains lines up-to XYZ
and rest of the lines in file2
.
Best Answer
With
awk
you can do:Explanation: The first
awk
argument (out=file1
) defines a variable with the filename that will be used for output while the subsequent argument (largefile
) is processed. Theawk
program will print all lines to the file specified by the variableout
({print >out}
). If the patternXYZ
will be found the output variable will be redefined to point to the new file ({out="file2}"
) which will be used as target to print the subsequent data lines.References: