Discussion:
Extrace only 2 lines
v***@yahoo.com
2014-02-17 20:53:34 UTC
Permalink
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" ?
Bug Poster
2014-02-21 10:01:48 UTC
Permalink
Post by v***@yahoo.com
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
'
Davide Brini
2014-02-21 10:20:02 UTC
Permalink
Post by v***@yahoo.com
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" ?
Well, of course you can do

sed -n '/primary/p; /\*Slot/p'

but I suspect your real problem is different, as it happens so many times.
--
D.
Joel Hammer
2014-02-21 13:08:11 UTC
Permalink
This seems trivial.
1.Print first line
2.Search for Slot. Print this line and exit.

That's it?

Here is a script.

sed -n "1p;/Slot/{p;q}" yourdata
Post by v***@yahoo.com
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
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" ?
Loading...