How to extract text blocks seperated by blank line
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to extract text blocks seperated by blank line
Hi,
I have file which contains information written in blocks (every block is different). Is it possible to read every block one by one to another file (one block per file).
I need to read <block1> to file.tmp. Make some manipulations with it and then rewrite the contents of file.tmp with <block2> make some manipulations with content of file.tmp and rewrite file.tmp with <block3> and initiate some manipulations with it. And so on....
Please give a bit more detail on what you are trying to do. e.g. why move blocks to a temp file---why not do the manipulations in place?
Every block can contain different information (block formatting and line count is different too). The reason why I want to operate with each block is to run some if statements for each block to check the format and according on that grep needed info.
An example
BLOCK_1
Item: $item1
unnecessary info
Time: $time1
unnecessary info
unnecessary info
Type: $type1
unnecessary info
$name1
unnecessary info
unnecessary info
unnecessary info
<blank line>
BLOCK_2
Item: $item2
Time: $time2
unnecessary info
Type: $type2
unnecessary info
unnecessary info
unnecessary info
{$destination} # in case of $destination {} are used
unnecessary info
<blank line>
Could you please explain what you have written I am not familiar with gawk, but I really would like to learn it. Maybe you could provide some useful tutorials?
Guys, Thanks a lot for help. But I have another question. Tried to resolve it by my own, but unfortunately without success. With your script the output is the following ( _ means space ) :
Is it possible to make such kind of formatting in this script? If it is not possible inside the script, what workaround should I use to make this thing work?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.