Re: Extrace only 2 lines

Bug Poster <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
18.02.2014 07:53, [email protected] wrote‚:
> Sample data 
> 
> primary
>    Factory   CTS 1.9.6(2) P1
>   *Slot 1    CTS 1.9.6(2) P1
>    Slot 2    CTS 1.7.1(4864) P1
>   loads file information
>     Factory  (cmterm-CTS.1-9-6-2R-K9.P1)
>         CTS: CTS.1-9-6-2R-K9.P1.sbn
>       Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
>     Slot 1   (cmterm-CTS.1-9-6-2R-K9.P1)
>         CTS: CTS.1-9-6-2R-K9.P1.sbn
>       Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
>     Slot 2
>       No loads file
> 
> 
> I know that 
> 
> sed '/primary/,/\*/!d' file gives me 
> primary
>    Factory   CTS 1.9.6(2) P1 
>   *Slot 1    CTS 1.9.6(2) P1 
> 
> But . how do I extract only the primary line and first "*Slot" ?
> 

#!/bin/bash

s="
x
primary
   Factory   CTS 1.9.6(2) P1
  *Slot 1    CTS 1.9.6(2) P1
   Slot 2    CTS 1.7.1(4864) P1
  loads file information
    Factory  (cmterm-CTS.1-9-6-2R-K9.P1)
        CTS: CTS.1-9-6-2R-K9.P1.sbn
      Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
    Slot 1   (cmterm-CTS.1-9-6-2R-K9.P1)
        CTS: CTS.1-9-6-2R-K9.P1.sbn
      Touch: CTSDEV.1-9-6-2R-K9.P1.SPA
    Slot 2
      No loads file
"

echo "$s" | sed -n '
    /primary/! b
    p
    :a /\*/! { n; ba }
    s/ *\([^ ]*\).*/\1/p
    q
'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.