Help with sort in Linux

Hi everyone,
I have a text file with this following format:

w m a c G
 + V b y
 + d f e t

I'd like to sort it to a file with the following format (same number of lines, same number of fields, but all fields are sorted alphabetically)

 G V a b c
 + d e f
 + m t w y

I thought of a combination of sort, paste or sed command steps. Could anyone help ?
Thanks a lot,
Roseriver

I'm struggling to think of an alphabetic sort order where V comes before a b c. Even as ASCII numbers V comes last.

In ascii capital letters preceed low-case letters: ascii

I this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum and must include a completely filled out homework template from the special homework rules.

If you did post homework in the main forums, please review the guidelines for posting homework and repost. If not, please explain why you need to sort items this way. And, please explain more clearly what you are trying to do...

In what manner of sort does a <+> character sort between <c> and <d> and also sort between <f> and <m>?

Hi Don,
To answer your "rude" question:
1) This is the "real world" format of a subcircuit's pin order in Verilog or
CDL netlist. And I want to sort them in alphabetically order to be used in different environment such as Cadence.
example:

 
 .SUBCKT analog_top cfg_wait[2] cfgwait[1] iso_0p9
 + d_idac_div[1] d_mux_div  rx_reserved[3]
 + VDD1 GND2
 

2) The + character actually is similar to new line character. I am not sure why the netlist have it that way...
3) I was able to manually using nedit text editor to do what I want.
But it took me a while since I have to merge all fields into one line then sort it then break it into many lines again...
I just want to ask Linux experts if there is a better way of doing it ....
Best regards,

Hi. I'm sorry you found the question rude. There are a few students who run into this forum hoping to get us to do their homework for them. When that happens it is bad for us and for them.

In your above example, is .SUBCKT also "special" in that it must appear first in the output no matter what else follows, or should it be sorted too? The way you are describing the <+> character, it sounds like a continuation character indicating that it is an extension of the previous line. Can your input files contain more than one Verilog or CDL pin order list that should be sorted separately?

Assuming that there can be multiple subcircuit pin orders in a single file (with blank lines between them being ignored) and that the .SUBCKT string doesn't need to be treated specially, you could start with something like:

#!/bin/ksh
IAm=${0##*/}
tmpfile="$IAm.$$"
trap 'rm -f "$tmpfile"' EXIT

awk -v tmpfile="$tmpfile" '
BEGIN {	cmd = "sort > " tmpfile
}
function print_list(	data, i, j) {
	close(cmd)
	for(i = first; i <= last; i++) {
		if(i != first)
			printf(" +")
		for(j = 1 + (i != first); j <= count; j++) {
			getline data < tmpfile
			printf(" %s%s", data, (j == count) ? "\n" : "")
		}
	}
	close(tmpfile)
}
NF == 0 {
	next
}
$1 != "+" {
	if(NR > 1)
		print_list(first, last)
	count[first = last = NR] = NF
	for(i = 1; i <= NF; i++)
		print $i | cmd
	next
}
{	count[last = NR] = NF
	for(i = 2; i <= NF; i++)
		print $i | cmd
}
END {	print_list()
}' file

which, if file contains:

w m a c G
 + V b y
 + d f e t

 .SUBCKT analog_top cfg_wait[2] cfgwait[1] iso_0p9
 + d_idac_div[1] d_mux_div  rx_reserved[3]
 + VDD1 GND2

(as in your two examples) produces the output:

 G V a b c
 + d e f
 + m t w y
 .SUBCKT GND2 VDD1 analog_top cfg_wait[2]
 + cfgwait[1] d_idac_div[1] d_mux_div
 + iso_0p9 rx_reserved[3]

This was written and tested using the Korn shell, but should work with any shell that recognizes basic POSIX-required shell syntax (such as ash , bash , dash , ksh , zsh , and several others) but not a pure Bourne shell and not a shell based on csh syntax.

Obviously, error checking should be added to the above if you're going to use this in a production environment, but I'll leave that as an exercise for the reader.

If someone else want to try this on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk .

Hi Don,
Thank you. Much appreciated.
Best regards,
Rose