reversing a list

Hi Everyone,
I am reading in a list of IDs from a file that is is chronological order. My current code in simplified form looks like this (ksh by the way)

IDS=`awk -F\| '{print $1}' inputfile.txt`
for i in $IDS
do
do various things with that ID
done

       inputfile.txt looks like this:

1|stuff|more stuff|etc
2|stuff|more stuff|etc
3|stuff|more stuff|etc
.
.
.
10000|stuff|more stuff|etc

What I would like to do is start with the last ID (10000) and work towards the first ID (1). This is an oversimplified example. There are gaps between IDs, the may not be consecutive. I cannot do a while loop starting with i=10000 and decrement to 1 due to this. Any ideas how I could reverse the values in $IDS before the for loop? The input file can be manipulated prior to calling the script. I thought there was a command to invert a list but I can't remember what it is and can't find it ( I searched the forum + google + man -k)
Thanks,
Tony

Don't you hate when you search for ever, can't find the answer, then as soon as you ask someone else, you find it. The command I was think of was 'rev'. However, I still need some help if you can offer it. rev exists on my HPUX box but not on my Solaris box where the script runs. Anyone know where I can get a similar binary for Sun?

I feel like I am having a conversation with myself, feel free to jump in.

The rev command did not do what I had remembered. It reverses the characters on 1 line, not the entire file or list.

Any other ideas? I am working on builting a script to reverse the lines using wc output and sed p in a loop.

Here is the script if anyone is interested, however it isn't the most efficient.

#!/usr/bin/ksh
LINES=`wc -l $1| awk '{print $1}' | sed 's/ //g'`
while [ $LINES -ge 1 ]
do
sed -n "$LINES p" $1
LINES=`expr $LINES - 1`
done

If anyone finds the equivalent binary that basically reads the file from bottom to top, please post and let me know.
Thanks,
TioTony

awk -F\| '{printf "%6s\n",$1}' inputfile.txt |
sort -r |
while read i
  do
  echo "Now processing $i ..."
  done

awk prints field 1 right-justified for proper sorting (reversed). Example output:

Now processing 158 ...
Now processing 75 ...
Now processing 3 ...
Now processing 2 ...
Now processing 1 ...

Hi Jimbo,
Thanks, I think the sort -r will run faster then the sed '<address> p'. I will give it a try tomorrow.
Thanks,
Tony

How about:
nl -ba inputfile | sort -nr | cut -f2-

I thought about using nl when I wrote the sed script but then thought I could just use sed without having to add the numbered lines. For my purposes, the sort -r will work.
Thanks,
TioTony

Also, some systems have "tac" installed...
Think "cat" in reverse :slight_smile: