View previous topic :: View next topic |
Author |
Message |
PietdeBoer Apprentice
Joined: 20 Oct 2005 Posts: 244 Location: Eindhoven, the Netherlands
|
Posted: Mon Oct 08, 2012 1:15 pm Post subject: [SOLVED] Extract a block of lines out of file |
|
|
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 |
|
|
Hypnos Advocate
Joined: 18 Jul 2002 Posts: 2889 Location: Omnipresent
|
Posted: Mon Oct 08, 2012 3:21 pm Post subject: |
|
|
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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10587 Location: Somewhere over Atlanta, Georgia
|
Posted: Mon Oct 08, 2012 3:22 pm Post subject: |
|
|
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 _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
ulenrich Veteran
Joined: 10 Oct 2010 Posts: 1480
|
Posted: Mon Oct 08, 2012 3:33 pm Post subject: |
|
|
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
|
|
|
Back to top |
|
|
Hu Moderator
Joined: 06 Mar 2007 Posts: 21431
|
Posted: Tue Oct 09, 2012 1:57 am Post subject: |
|
|
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 |
|
|
dmitchell Veteran
Joined: 17 May 2003 Posts: 1159 Location: Austin, Texas
|
Posted: Tue Oct 09, 2012 4:11 am Post subject: |
|
|
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 |
|
|
khayyam Watchman
Joined: 07 Jun 2012 Posts: 6227 Location: Room 101
|
Posted: Tue Oct 09, 2012 7:54 am Post subject: |
|
|
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 |
|
|
PietdeBoer Apprentice
Joined: 20 Oct 2005 Posts: 244 Location: Eindhoven, the Netherlands
|
Posted: Thu Oct 11, 2012 11:07 am Post subject: |
|
|
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 |
|
|
|