Extract block of text
am 24.01.2008 19:24:40 von joao.manuel.rei
Hi
I would like to extract the first block of data in a file. Input
example with 2 blocks:
This is the begining
-------
some text1
some text2
This is the beggining
-------
some text1
some text2
In this example the line count of each "input block" is 4. But the
size can be different. It's dynamic. The blocks are similar.
Is It better to do it with shell, sed or awk?
Thanks
Jo=E3o Rei
Re: Extract block of text
am 24.01.2008 19:51:49 von Janis Papanagnou
joao.manuel.rei@gmail.com wrote:
> Hi
>
> I would like to extract the first block of data in a file. Input
> example with 2 blocks:
>
> This is the begining
> -------
> some text1
> some text2
> This is the beggining
> -------
> some text1
> some text2
>
> In this example the line count of each "input block" is 4. But the
> size can be different. It's dynamic. The blocks are similar.
Okay, fine; but what is the _criterion_ for a new block?
A line before a dashed line? Or some specific heading text?
Or is it sufficient to call your script with a different
number as argument each time?
Simple solutions for the latter might be...
awk 'NR<=4' datafile
sed -n 1,4p datafile
(or any more efficient variant thereof).
>
> Is It better to do it with shell, sed or awk?
A general purpose tool for such tasks is typically awk.
Janis
>
> Thanks
>
> João Rei
Re: Extract block of text
am 24.01.2008 19:53:49 von Ed Morton
On 1/24/2008 12:24 PM, joao.manuel.rei@gmail.com wrote:
> Hi
>
> I would like to extract the first block of data in a file. Input
> example with 2 blocks:
>
> This is the begining
> -------
> some text1
> some text2
> This is the beggining
> -------
> some text1
> some text2
>
> In this example the line count of each "input block" is 4. But the
> size can be different. It's dynamic. The blocks are similar.
>
> Is It better to do it with shell, sed or awk?
>
Don't use shell for text processing, and only use sed for simple substitutions.
awk 'f{print p} /---/&&f++{exit} {p=c;c=$0}' file
Ed.
Re: Extract block of text
am 24.01.2008 23:42:26 von joao.manuel.rei
Excelent answer.
Solved.
Thanks.
Joao Rei
Ed Morton wrote:
> On 1/24/2008 12:24 PM, joao.manuel.rei@gmail.com wrote:
> > Hi
> >
> > I would like to extract the first block of data in a file. Input
> > example with 2 blocks:
> >
> > This is the begining
> > -------
> > some text1
> > some text2
> > This is the beggining
> > -------
> > some text1
> > some text2
> >
> > In this example the line count of each "input block" is 4. But the
> > size can be different. It's dynamic. The blocks are similar.
> >
> > Is It better to do it with shell, sed or awk?
> >
>
> Don't use shell for text processing, and only use sed for simple substitutions.
>
> awk 'f{print p} /---/&&f++{exit} {p=c;c=$0}' file
>
> Ed.
Re: Extract block of text
am 25.01.2008 08:12:09 von Stephane CHAZELAS
On Thu, 24 Jan 2008 12:53:49 -0600, Ed Morton wrote:
[...]
> Don't use shell for text processing, and only use sed for simple substitutions.
>
> awk 'f{print p} /---/&&f++{exit} {p=c;c=$0}' file
[...]
Excellent advice, but for clarification, the line above is a
shell command line. So, yes, use the shell, but use it for what
it is made for, that is to run commands, and if possible, the
right command for the job. For complicated text processing, awk
is most of the times the best answer (if you are not
ready/willing to move on to perl/ruby).
--
Stephane