1

I've got some big text log files. The content is like:

Begin to work
Load library
Start
TEXTLOG
Checking
ok
TEXTLOG
Start process
Starting node
ok
TEXTLOG
Stop node
TEXTLOG

In this file the lines "TEXTLOG" serves as delimeter, so I wish to split this file into several smaller files, using "TEXTLOG" as EOF indicator, so I should files:

file1:

Begin to work
Load library
Start

file2:

Checking
ok

file3:

Start process
Starting node
ok

file4:

Stop node

How can I achieve this using shell? Thanks.

3 Answers 3

2

Using GNU awk:

awk -v RS="\nTEXTLOG\n" '{print > "file"++c}' file

RS is the record separator and allows to get the content of each file at once.

Sign up to request clarification or add additional context in comments.

Comments

2

Using csplit:

csplit --suppress-matched file "/TEXTLOG/" "{*}"
  • --suppress-matched makes sure the TEXTLOG separator is not included in the output
  • "{*}" tells csplit to run the matching pattern not just once, but throughout the entire file

2 Comments

seems my csplit on linux/mac doesn't support --suppress-matches. How did you try?
I'm running Ubuntu 18.04. csplit --version gives me csplit (GNU coreutils) 8.28.
1

Try this :

awk 'BEGIN{c=1}/^TEXTLOG/{c++;next} {print > "file"c}' file 
ls 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.