Copy all before the first blank line

Asked

Viewed 143 times

6

I have a file with several text blocks, separated by blank line. Ex.:

block1
block1

block2

block3
block3

I need a solution with sed, awk or perl that locates the first blank line and redirects the previous block to another file and so on until the end of the file.

I have this sed command that locates the first block, but not the rest: sed -and '/. /! Q'

Can someone help me?

2 answers

2

The following command creates files "output1", "output2", etc., with each block.

awk -v RS="" '{print > "output" NR }' input.txt

Explanation:

  • RS="" records separated by one or more blank lines
  • print > "output" NR writes the block in output+register number.

This is what you want?

  • Exactly John, this is what I needed. Thank you.

0

I’m not sure if I understand correctly, but if your goal is to remove the blank lines, you can use the following command:

sed '/^\s*$/d'
  • Renato, I need to locate the blank line and move everything that comes before it to another file, don’t delete it.

  • Only from the first block? Or VC wants to integrate by the file and generate multiple files ?

  • I need to generate several acquisitions, one for each existing block in the base file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.