help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Printing sections from files with sed


From: Greg Wooledge
Subject: Re: Printing sections from files with sed
Date: Tue, 31 Jan 2023 07:46:21 -0500

On Tue, Jan 31, 2023 at 07:49:02AM +0100, Hans Lonsdale wrote:
> I want to print lines according to the following description
> 
>   ## FM [TOBIN]
>   ## Some text
>   ## More text
>   ## END OF FM [TOBIN]
> 
> I provide 
> 
> faml="FM"
> asmb="TOBIN"

I'm guessing that the "  ## " is not actually part of the text.  If it is,
please provide an actual sample of the input.  Ideally as an attachment,
so that you don't feel a need to add indentation or markup.

Here's a simple awk parser that does the job:

awk -v marker="$faml [$asmb]" '
  BEGIN                  {mark=0}
  $0 == marker           {mark=1}
  mark                   {print}
  $0 == "END OF " marker {exit}
'

This is the result you expect, right?

unicorn:~$ cat foo
#!/bin/sh
faml="FM"
asmb="TOBIN"
exec awk -v marker="$faml [$asmb]" '
  BEGIN                  {mark=0}
  $0 == marker           {mark=1}
  mark                   {print}
  $0 == "END OF " marker {exit}
' "$@"
unicorn:~$ cat bar
some lines
FM [ROBIN]
here
FM [TOBIN]
payload
line two
END OF FM [TOBIN]
END OF FM [ROBIN]
unicorn:~$ ./foo bar
FM [TOBIN]
payload
line two
END OF FM [TOBIN]
unicorn:~$ 

I suppose this could also be done with sed, but I wouldn't attempt it.
awk is much easier for this kind of task.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]