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
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
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.
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
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
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...
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