From here Split portion of string in bash, with some code changes, I managed to achieve the goal.
Now, I want to save the text in separate file.
I tried:
awk '/[code]:/{flag=1} flag; /[/code]:/{flag=0}{x="/home/user/split/File" i".txt";}{print > x;}' /home/user/bigfile.nfo
but I got many files with one or no line (empty file with 0 bytes)
How to write all content between [code]
and [/code]
to separate file ? As many text found between those tag, as many files should be created, that's my expectation
Where is my mistake in code ?
The bigfile content
blavbl
[code]
sdasdasd
asdasd
...
[/code]
line X
line Y
etc
...
[code]
...
test
test
[/code]
blabla
[code]
Single line
[/code]
After ran script I get some files with one line instead all text between blocks
I expect to have
File1.txt
sdasdasd
asdasd
...
File2.txt
...
test
test
File3.txt
Single line
Etc
CodePudding user response:
A few issues with OP's current code:
- the characters
[
,]
and/
have special meaning inawk
regex patterns; one solution is to escape said characters when looking for them as literal characters - OP should make sure a file's descriptor is closed once no more output is going to said file (this should keep
awk
from (potentially) crashing due to 'out of file descriptor' errors) - OP's current patterns include a trailing
:
but no such character exists in OP's sample input (ie,[code]:
will not match[code]
)
One awk
idea:
awk '
/^\[code\]/ { outfile="/home/user/split/File" i ".txt"; next }
/^\[\/code\]/ { close(outfile); outfile=""; next }
outfile { print > outfile }
' bigfile.nfo
NOTE: technically ]
(sans the escape \
) should also work
This generates:
$ head File*.txt
==> File1.txt <==
sdasdasd
asdasd
...
==> File2.txt <==
...
test
test
==> File3.txt <==
Single line