Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Extract a block of lines out of file
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
PietdeBoer
Apprentice
Apprentice


Joined: 20 Oct 2005
Posts: 244
Location: Eindhoven, the Netherlands

PostPosted: Mon Oct 08, 2012 1:15 pm    Post subject: [SOLVED] Extract a block of lines out of file Reply with quote

Hey Guys,

I would like to process blocks of text in a file, the file looks like this:

Code:
Command: batch-execute user1

user1 some information
luser2 some information

Command: batch-execute user2

user1 some information
user2 some information

Command: batch-execute user3

user1 some information
user 2 some information


I would like to put every block starting with "Command: ..." in a variable or textfile so I can process it further.

How could i achieve this? I've read something about awk and sed that use rangs delimiters.. no clue how to implement this however..
_________________
_ Got Root? _


Last edited by PietdeBoer on Mon Oct 15, 2012 1:56 pm; edited 1 time in total
Back to top
View user's profile Send private message
Hypnos
Advocate
Advocate


Joined: 18 Jul 2002
Posts: 2868
Location: Omnipresent

PostPosted: Mon Oct 08, 2012 3:21 pm    Post subject: Reply with quote

Do you have access to Perl for this task?

You can just scan the file line by line, and save the text starting with one "command" statement until you encounter the next one.
_________________
Personal overlay | Simple backup scheme
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7854
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon Oct 08, 2012 3:22 pm    Post subject: Reply with quote

Sed won't do this. There's no built-in syntax for AWK, either, but it's fairly easily implementable. It's a programming task, not a clever syntax task. Something like this:
Code:
#!/usr/bin/awk

$1 == "Command:" {
    if (FileIsOpen)
        close(FileName);
    FileName = sprintf("File%04u.txt", ++FileNumber);
    FileIsOpen = 1;
}

{
    print >>FileName;
}
Does this do what you want?

Edit: Hypnos is right. This is approximately the same amount of work in Perl. What language are you going to "process it further" in?

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
ulenrich
Veteran
Veteran


Joined: 10 Oct 2010
Posts: 1162

PostPosted: Mon Oct 08, 2012 3:33 pm    Post subject: Reply with quote

Code:
#!/bin/bash
export n=1000
cat "$1" | while read l; do
    [ "${l:0:7}" = "Command" ] && ((n+=1))
    [[ $n -gt 1000 ]] && echo "$l" >>"$n"
done

_________________
fun2gen2
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 9066

PostPosted: Tue Oct 09, 2012 1:57 am    Post subject: Reply with quote

ulenrich wrote:
Code:
export n=1000
No need to export an internal control variable.
ulenrich wrote:
Code:
cat "$1" | while read l; do
UUOC. You should instead redirect stdin from the file.
Back to top
View user's profile Send private message
dmitchell
Veteran
Veteran


Joined: 17 May 2003
Posts: 1159
Location: Austin, Texas

PostPosted: Tue Oct 09, 2012 4:11 am    Post subject: Reply with quote

Code:
sed '/^$/d' < file | split -l3

This deletes empty lines and splits the file into chunks of three lines each, placing each chunk in a separate file.
_________________
Your argument is invalid.
Back to top
View user's profile Send private message
khayyam
Advocate
Advocate


Joined: 07 Jun 2012
Posts: 2245

PostPosted: Tue Oct 09, 2012 7:54 am    Post subject: Reply with quote

PietdeBoer, et al ...

Similar to John's solution, split the file based on a pattern ("^Command") ...

Code:
awk '/^Command/{close("output"f".txt");f++}{print $0 > "output"f".txt"}' input.txt

best ... khay
Back to top
View user's profile Send private message
PietdeBoer
Apprentice
Apprentice


Joined: 20 Oct 2005
Posts: 244
Location: Eindhoven, the Netherlands

PostPosted: Thu Oct 11, 2012 11:07 am    Post subject: Reply with quote

khayyam wrote:
PietdeBoer, et al ...

Similar to John's solution, split the file based on a pattern ("^Command") ...

Code:
awk '/^Command/{close("output"f".txt");f++}{print $0 > "output"f".txt"}' input.txt

best ... khay


Thanks All!

Splitting the file up using the "Command" rule works!
_________________
_ Got Root? _
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum