Script to print first and last line have two pattern

Hello Guys,
I have file like
aaa
bbb
Line 1
cc
dd
Ser Nu 11223
ee
ff
Line 2
hh
hh
Ser Nu 44556
ii
jj

I need to match line starting with Line and Ser and only display them like

Line 1 Ser Nu 11223 | Line 2 Ser Nu 44556

The number of lines can be multiple Line 1 Line 2 Line 3 Line 4 ........Line 1000

---------- Post updated at 05:58 PM ---------- Previous update was at 04:44 PM ----------

sed -n '/Line/,/Ser/p'
this prints all the lines between the Line and Ser.
A small tweek is needed.........any takers.

awk '/^Line/{s=$0;S++}/^Ser/{printf "%s%s %s",(S>1)?" | ":"",s,$0}' file

---------- Post updated at 05:59 PM ---------- Previous update was at 05:58 PM ----------

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

______________________________________________________________________________
awk '/^Line/{s=$0;S++}/^Ser/{printf "%s%s %s",(S>1)?" | ":"",s,$0}' aa
awk: syntax error near line 1
awk: illegal statement near line 1
_____________________________________________________

SunOS last-call-5 5.10 Generic_125100-10
______________________________________________________________________________

aa is the file name

Try /usr/xpg4/bin/awk

Thanks Danmero.
It worked.
If you have a moment to spare any explanation would be great :slight_smile:
Sorry for being greedy :slight_smile: