Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Request Help w/awk script that sorts 1

Status
Not open for further replies.

Kipnep70

Technical User
Nov 18, 2003
81
US
I use the following awk script to create 5 columns out of a single column:

Code:
BEGIN {
  count=0
  cols=5
  }

{
len=length($0)
pad=substr("               ",1,15-len)

LINE=LINE " " $0 pad
count=count+1

if (count == cols) {
    print LINE
    count=0
    LINE=""
    }
}

END { if (count > 0) {
  print LINE}
  }

How would I modify the script so that each column is sorted downward and not across?

This is what i'm running right now:

Code:
cat MyOneColumnList.txt | sort | awk -f mkCol.awk


Current output:

U10021 U10027 U10028 U10034 U10062
U10072 U10073 U10082 U10107 U10166
U10171 U10252 U10258 U10510 U10511
.. .. .. .. ..
.. .. .. .. ..
Desired output:

U10021 U10072 .. .. ..
U10027 U10073 .. .. ..
U10028 U10082 .. .. ..
U10034 U10107 .. .. ..
U10062 U10166 .. .. ..


You get the point.

.. = UNNNNN

 
Something like this perhaps, you could either add it to the end of your pipeline or adapt it into your script. Note the use of sprintf() formatting to achieve the desired column widths.

Code:
awk '
        {
                for (i=1; i<=NF; i++) { a[i]=sprintf("%s%-15s",a[i],$i) }
        }
        END {
                for (i=1; i<=NR; i++) { print a[i] }
        }
'

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top