Sorting content between match pattern and move on with awk and sed

S 0.0 0.0 (reg, inst050)
k
e
f
d
c

S 0.0 0.0 (mux, m030)
k
g
r
s
x
v

S 0.0 0.0 (reg, inst020)
q
s
n
m

S 0.0 0.0 (rpt13, rpt13)
p
e
m
a
x
z
l
t
f
g

S 0.0 0.0 (INPUT)
t
g
h
w
d

can i have an ideal on 2 cases with sed/awk by sorting the content between S 0.0 and empty space

Please post the expected output.

thanks. the output results should look like this

S 0.0 0.0 (reg, inst050)
c
d
e
f
k

S 0.0 0.0 (mux, m030)
g
k
r
s
v
x

S 0.0 0.0 (reg, inst020)
m
n
q
s


S 0.0 0.0 (rpt13, rpt13)
a
e
f
g
l
m
p
t
x
z

S 0.0 0.0 (INPUT)
d
g
h
t
w

And why do you need two cases with sed and awk ?

Is this a homework assignment?

What have you tried so far?

i had anther post 6 hrs ago with different question, and the solution was with sed. that is the reason i would stay with sed for my solution but in this example, the resources i could find was mostly with awk.
Print lines between two patterns , the awk way

i asked because it could help me in my daily task. not school assignment.

Try

awk 'NF==1 {print | "sort"} NF!=1 {close ("sort"); print}' file
1 Like

hi Rudic
that is very powerful single line solution, would you mind to explain to me how it actually does?

This is specific to the specific input sample you gave; nevertheless it should work on other structures after some adaption.

awk     'NF==1  {print | "sort"}        # one field: record member; add to sort input via pipe; sort will print to stdout when pipe is closed.
         NF!=1  {close ("sort"); print} # record header or empty line; close the sort pipe to finish sorting the record; print header to stdou
        ' file

what is the content is not one field?
example

S 0.0 0.0 (reg, inst050)
kfdasdf dsaf
easd sadfas 
ffadsfasfdsadsa  dsafdsafas
dd8385fdsa asdasdfaf2432vdsaf 
dsafcfasfdsa f32f32asf322dsdsaf

If there are only whitespace characters on a line, the number of fields is 0. Otherwise, the number of fields is the number of sequences of non-whitespace characters separated by sequences of whitespace characters.

This is why it is so important to give us representative sample input for the problem you want to solve...

Still making some wild assumptions based on the sample input you have shown us, try:

awk '
!NF || /^S .*[(].*[)]$/ {
	# Close pipe to sort when we get a blank line or a section header.
	close("sort")
	# Copy the blank or section header line to standard output.
	print
	next
}
{	# Feed other lines to sort.
	print | "sort"
}' file
1 Like

thanks Don Cragun.
you just give me hint on how i should proceed with my several cases and i just made it. thanks.

thanks to RudiC on the example. i have fun with it.

now i can produce what i want.. it save me a lot of efforts. thanks guys.

Whenever you can distinguish record headers/separators from the record data, that code snippet will work. Don Cragun refined the conditions from your very specific sample to a more general one; for other data structures you need to come up with your own, new conditions...

1 Like

Just for your info:
with GNU awk 4+ you don't need an external program for sorting:

awk 'BEGIN {
  # scan arrays by values ASCIIbetical order  
  PROCINFO["sorted_in"] = "@val_str_asc"
  }
{
  # split $0 by NL
  n = split($0, t, "\n")
  print t[1]; delete t[1]
  # output in order
  for (e in t)
    print t[e]
  # print the logical RS    
  print ""    
  }' RS= infile 

With Perl:

perl -F'\n' -lan00e'
  print join "\n", shift @F, (sort @F), $/
   ' infile
1 Like