LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2009, 06:38 AM   #1
art84_LV
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Rep: Reputation: 0
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).

The input is something like this


<block1>
<empty line>
<block2>
<empty line>
...
...
...
<block25>
<empty line>
<block26>

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....

Any ideas?
 
Old 11-12-2009, 06:53 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Welcome to LQ!!

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?
 
Old 11-12-2009, 07:15 AM   #3
art84_LV
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
Welcome to LQ!!

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>

OUTPUT FILE:
$time1 $item1 $type1 $name
$time2 $item2 $type2 $destination

As you see $time, $item and $name can be extracted using simple grep (grep Time, grep Item, grep Type) the problem is with extracting other variables.

Last edited by art84_LV; 11-12-2009 at 07:42 AM.
 
Old 11-12-2009, 07:40 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use gawk
Code:
awk -vRS= -vFS="\n" '{
  for(i=1;i<=NF;i++){
    gsub(/\#.*/,"",$i)
    if ( $i~/item|time|type|name|destination/){
        printf $i" "
    }
  }
  print ""  
}' file
output
Code:
$ ./shell.sh
$item1 $time1 $type1 $name1
$item2 $time2 $type2 {$destination}
 
Old 11-12-2009, 08:03 AM   #5
art84_LV
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
use gawk
Code:
awk -vRS= -vFS="\n" '{
  for(i=1;i<=NF;i++){
    gsub(/\#.*/,"",$i)
    if ( $i~/item|time|type|name|destination/){
        printf $i" "
    }
  }
  print ""  
}' file
output
Code:
$ ./shell.sh
$item1 $time1 $type1 $name1
$item2 $time2 $type2 {$destination}
It works, GREAT, thanks a lot.

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?
 
Old 11-12-2009, 08:15 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by art84_LV View Post
but I really would like to learn it. Maybe you could provide some useful tutorials?
see my sig for gawk link.
 
Old 11-12-2009, 11:22 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
and of course one of the best tutorial sites around:
http://www.grymoire.com/Unix/
 
Old 11-16-2009, 08:17 AM   #8
art84_LV
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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 ) :

Item___: $item1 $time = 2009.15.11 9:42:15.397927000 Type___: $type1 $name

And I want to make it as following:

$time = 9:42:15 Item: $item1 Type: $type1 $name

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?

Last edited by art84_LV; 11-16-2009 at 08:29 AM.
 
Old 11-16-2009, 05:36 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show what you have tried.
 
Old 11-17-2009, 01:02 AM   #10
art84_LV
LQ Newbie
 
Registered: Nov 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
show what you have tried.
I don't store unsuccessful jobs. I have tried to manipulate output format in printf line ))
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
have a Variable with multiple lines of data need to have it all on one line seperated xskycamefalling Programming 11 05-12-2009 04:44 AM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
Extract certain text info from text file xmrkite Linux - Software 30 02-26-2008 11:06 AM
script to extract blocks of text from many files. gruessle Programming 4 10-19-2007 02:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration