Sergey Kovalev
Sergey Kovalev

Reputation: 893

split text file in two using bash script

I have a text file with a marker somewhere in the middle:

one
two
three
blah-blah *MARKER* blah-blah
four
five
six
...

I just need to split this file in two files, first containing everything before MARKER, and second one containing everything after MARKER. It seems it can be done in one line with awk or sed, I just can't figure out how.

I tried the easy way — using csplit, but csplit doesn't play well with Unicode text.

Upvotes: 8

Views: 6792

Answers (4)

ghostdog74
ghostdog74

Reputation: 343191

you can do it easily with awk

awk -vRS="MARKER" '{print $0>NR".txt"}' file

Upvotes: 12

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102458

Try this:

awk '/MARKER/{n++}{print >"out" n ".txt" }' final.txt

It will read input from final.txt and produces out1.txt, out2.txt, etc...

Upvotes: 5

Dennis Williamson
Dennis Williamson

Reputation: 360693

sed -n '/MARKER/q;p' inputfile > outputfile1
sed -n '/MARKER/{:a;n;p;ba}' inputfile > outputfile2

Or all in one:

sed -n -e '/MARKER/! w outputfile1' -e'/MARKER/{:a;n;w outputfile2' -e 'ba}' inputfile

Upvotes: 3

Marcelo Cantos
Marcelo Cantos

Reputation: 186118

The split command will almost do what you want:

$ split -p '\*MARKER\*' splitee 
$ cat xaa
one
two
three
$ cat xab
blah-blah *MARKER* blah-blah
four
five
six
$ tail -n+2 xab
four
five
six

Perhaps it's close enough for your needs.

I have no idea if it does any better with Unicode than csplit, though.

Upvotes: 1

Related Questions