1

I have this block in a text file. In vi (switching on the visualization of the end-of-line characters with :set list as per View line-endings in a text file) it reads

 SHARE_INFO_FOR: SHARE/u_MYGROUP/$
 USER/GROUP   SHARES  PRIORITY  STARTED  RESERVED  CPU_TIME  RUN_TIME   ADJUST$
 u_zc        10000    3333.333     0        0         0.0        0       0.000$
 $
 SHARE_INFO_FOR: SHARE/u_MYSECONDGROUP/$

If I try to isolate this with sed I would use a multiple lines regexp

 sed -rn '/SHARE\/u_MYGROUP\//{:a;N;/^$/{/.*/p;d};ba}'

sed does not identifies the ending line of the block. whereas if I explicitly put the content of if next non-empty line it works.

 sed -rn '/SHARE\/u_MYGROUP\//{:a;N;/SHARE\/u_MYSECONDGROUP\//{/.*/p;d};ba}'

My problem is of course that in general I do not know what is the group name in the next line so I wanted to delimit the interesting block using the name as a beginning marker and the empty line as end of block marker.

Do you see why ^$ is not working as a delimiter for the regexp in sed?

Community
  • 1
  • 1
Rho Phi
  • 1,182
  • 1
  • 12
  • 21

2 Answers2

2
sed -n '\#SHARE/u_MYGROUP/#,/^$/{
 p
 }' YourFile

if it only need to print the block starting from line having SHARE/u_MYGROUP/ until next first empty line. It change the default separator (/) for pattern by (#) using a first escape char for this

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • This is actually much better! thanks a lot! I made it an inline command to put into a script by using `sed -n '\#SHARE/u_MYGROUP/#,/^$/{p;}' lastlog` It's too bad that I do not have enough reputation to vote up this answer. – Rho Phi Jul 16 '14 at 16:02
  • You could replace the `{ p }` with just `p`, avoiding multiple lines. Even if you need multiple actions grouped within `{ }`, you can often put them on one line with semicolons in between (and on Mac OS X, you need a semicolon after the last command and before the `}`). – Jonathan Leffler Jul 16 '14 at 16:31
0

All of the sed language constructs to do anything with multiple lines LITERALLY became obsolete in the mid-1970s when awk was invented:

$ awk -v RS= '/SHARE\/u_MYGROUP/' file
SHARE_INFO_FOR: SHARE/u_MYGROUP/
USER/GROUP   SHARES  PRIORITY  STARTED  RESERVED  CPU_TIME  RUN_TIME   ADJUST
u_zc        10000    3333.333     0        0         0.0        0       0.000
Ed Morton
  • 188,023
  • 17
  • 78
  • 185