Royce
Royce

Reputation: 585

Shellscript - read blockwise

I was wondering if it's possible to read from a file blockwise until a specific delimiter (i.e. a certain string or something), not linewise (as 'read' would do).

input.txt contains:

text1
text2
NEXT
text3
NEXT
text4
text5

Each text-section should be saved in a certain single file (text1+text2 // text3 // text4+text5)

I played around a bit with editing the IFS but had no success unfortunately.

Upvotes: 2

Views: 100

Answers (1)

anubhava
anubhava

Reputation: 785761

You can use gnu-awk with custom record separator:

awk -v RS='\nNEXT\n' '{sub(/\n$/, ""); print "<" $0 ">"}' file
<text1
text2>
<text3>
<text4
text5>

Each block in <...> is a single record.

Upvotes: 2

Related Questions