LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-19-2012, 09:13 AM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,399

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814

Quote:
Originally Posted by mrpurple View Post
I'd definitely be happy to hear of something more efficient.
In my experience, efficiency in shell scripts is usually comes from minimizing the amount of work done in the shell. In this case, I think awk will work a lot better (so we can do zero work in the shell):
Code:
#!/usr/bin/awk -f

BEGIN {
    RS = "\n(\n|$)";
    FS = "\n";
    block = 0;
}

$1 ~ /\[DATATYPE2\]/ {
    for (i = 2; i <= NF-1; i++) {
        printf("processing line %d of block %d\n%s\n",
            i - 2, block, $i);
    }
    block++;
}
Basically, RS = "\n(\n|$)" separates each block, and FS = "\n" puts each line of the block in $1, $2, $3, ... $NF (where $1 is the "[DATATYPE]" line and $NF is the "[END]" line.
 
Old 09-19-2012, 11:10 AM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Well it is a little rough but here is an awk script that does the whole lot. You can change what happens in the END loops to suit your self:
Code:
awk 'BEGIN{ RS = "\\[END[^]]*\\]\n+";FS = "\n"}{for(i = 2; i <= NF; i++)arr[b,c++] = $i;b++;c=0}END{for(x=0; x < b; x++){while( arr[x,y] )print x,y,arr[x,y++];y=0}}' file
 
  


Reply

Tags
array, awk, multiline, parse


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
parsing a text file - to awk or not to awk ? rollyah Programming 9 08-18-2011 02:20 PM
Get data from multi lined text file using awk, sed or perl - grep & cut not upto par cam34 Programming 4 07-02-2010 03:10 AM
How to remove specific text blocks in a file xfrantzis Linux - Software 1 07-01-2010 09:06 AM
No of inodes equals no of data blocks in a linux file sysem? Marty21 Linux - Kernel 4 01-10-2009 07:41 PM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM


All times are GMT -5. The time now is 06:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration