Sponsored Content
Full Discussion: Help with sort in Linux
Top Forums Shell Programming and Scripting Help with sort in Linux Post 302972294 by Don Cragun on Tuesday 3rd of May 2016 10:42:35 PM
Old 05-03-2016
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:
Code:
#!/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[i]; j++) {
			getline data < tmpfile
			printf(" %s%s", data, (j == count[i]) ? "\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:
Code:
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:
Code:
 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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

help newb at linux and bash need numeric script sort

I am trying to setup to automatically import a series of mysql database files. I am doing manually now and its a royal pain. All the sql files are sequentially numbered in a format of 4 numbers underscore text with spaces replaced by underscores. example: There are 3 databases each setup... (1 Reply)
Discussion started by: dlm1065
1 Replies

2. UNIX for Dummies Questions & Answers

Linux Sort command

Hello! Can anybody explain in laymen terms what the (+) option in the sort command for Linux does? Please. Thanks in advance!!:D (1 Reply)
Discussion started by: itisijayare
1 Replies

3. Shell Programming and Scripting

difference in unix vs. linux sort

Hi, I am using some codes that have been ported from unix to linux, and now the sorting no longer results in the desired ordering. I'm hoping to find a way to mimic the unix sort command in linux. The input file is structured the following: $> cat file.txt... (6 Replies)
Discussion started by: aj.schaeffer
6 Replies

4. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

5. UNIX for Dummies Questions & Answers

linux sort command produces strange output

cat a .a ba .b bb .c bc sort a .a .b ba bb bc .c NOTE: .a and .b appears before ba and bb, where as .c appears after bc. In general (3 Replies)
Discussion started by: ajb
3 Replies

6. Linux

sort command in centos linux os

Iam working on centos os. Iam not able to sort records without option Please help me out Jayaprakash B. (1 Reply)
Discussion started by: jpachar
1 Replies

7. UNIX for Dummies Questions & Answers

LINUX SORT command chops results

I am trying to sort a file . The file looks like this: DDFF 2 /ztpfrepos/pgr/load DDFQ 2 /ztpfrepos/pgr/load DDFX 2 /ztpfrepos/pgr/load DDUA 2 /ztpfrepos/pgr/load My command: sort -k1 /home/c153507/Bin/OPL1.txt -o /home/c153507/Bin/OPL1.txt The results are OK except for one line where... (4 Replies)
Discussion started by: Yahalom
4 Replies

8. Homework & Coursework Questions

linux sort command

This is the question being asked: (Sort your data file by last name first, then by the first name second - save as first_last.) I am not quite sure of the type of sort I am being asked to perform. I have read the man pages of the sort command a few times, as well as searching online for possible... (10 Replies)
Discussion started by: demet8
10 Replies

9. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

10. UNIX for Beginners Questions & Answers

Linux find jpg and sort by date

I want to find all jpg files and then sort them by modification date. This is where I started. find . -type f -name "*.jpg" I tried to pipe a sort in there but that did not seem to work. Do I need to use xargs? (10 Replies)
Discussion started by: cokedude
10 Replies
JOIN(1) 						      General Commands Manual							   JOIN(1)

NAME
join - relational database operator SYNOPSIS
join [ options ] file1 file2 DESCRIPTION
Join forms, on the standard output, a join of the two relations specified by the lines of file1 and file2. If file1 is `-', the standard input is used. File1 and file2 must be sorted in increasing ASCII collating sequence on the fields on which they are to be joined, normally the first in each line. There is one line in the output for each pair of lines in file1 and file2 that have identical join fields. The output line normally con- sists of the common field, then the rest of the line from file1, then the rest of the line from file2. Fields are normally separated by blank, tab or newline. In this case, multiple separators count as one, and leading separators are dis- carded. These options are recognized: -an In addition to the normal output, produce a line for each unpairable line in file n, where n is 1 or 2. -e s Replace empty output fields by string s. -jn m Join on the mth field of file n. If n is missing, use the mth field in each file. -o list Each output line comprises the fields specifed in list, each element of which has the form n.m, where n is a file number and m is a field number. -tc Use character c as a separator (tab character). Every appearance of c in a line is significant. SEE ALSO
sort(1), comm(1), awk(1) BUGS
With default field separation, the collating sequence is that of sort -b; with -t, the sequence is that of a plain sort. The conventions of join, sort, comm, uniq, look and awk(1) are wildly incongruous. JOIN(1)
All times are GMT -4. The time now is 01:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy