C save split string into a new file
You can use the csplit
command to split a file into multiple files based on a specified delimiter, and then redirect the output to a new file.
Here is an example:
csplit -z -f output_ file.txt '/pattern/' '{*}'
This will split the file file.txt
into multiple files named output_00
, output_01
, output_02
, etc., based on the pattern /pattern/
. The -z
option tells csplit
to use the specified delimiter instead of the default whitespace character.
You can also use the -n
option to specify the number of output files:
csplit -z -n 5 -f output_ file.txt '/pattern/' '{*}'
This will split the file into 5 output files.
Alternatively, you can use the split
command, which is similar to csplit
but does not support regular expressions:
split -l 1000 file.txt output_
This will split the file file.txt
into multiple files named output_
, output__
, output___
, etc., with each file containing 1000 lines.
You can also use the awk
command to split a file into multiple files based on a specified delimiter:
awk '{print > ("output_" NR ".txt")}' file.txt
This will split the file file.txt
into multiple files named output_1.txt
, output_2.txt
, output_3.txt
, etc., based on the newline character (\n
) as the delimiter.
Note that the above examples assume that the delimiter is a fixed string or a regular expression. If the delimiter is a variable or a complex pattern, you may need to use a more advanced tool or programming language to achieve the desired result.