reversing a list | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

reversing a list

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
 
Join Date: Oct 2001
Last Activity: 16 October 2014, 1:04 AM EDT
Location: Southern California
Posts: 332
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old 10-11-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
 
Join Date: Oct 2001
Last Activity: 16 October 2014, 1:04 AM EDT
Location: Southern California
Posts: 332
Thanks: 0
Thanked 1 Time in 1 Post
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?
Sponsored Links
    #3  
Old 10-11-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
 
Join Date: Oct 2001
Last Activity: 16 October 2014, 1:04 AM EDT
Location: Southern California
Posts: 332
Thanks: 0
Thanked 1 Time in 1 Post
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.
    #4  
Old 10-11-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
 
Join Date: Oct 2001
Last Activity: 16 October 2014, 1:04 AM EDT
Location: Southern California
Posts: 332
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #5  
Old 10-12-2002
Jimbo
Guest
 
Posts: n/a

Code:
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:

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

Sponsored Links
    #6  
Old 10-13-2002
TioTony's Avatar
TioTony TioTony is offline Forum Advisor  
Bit Pusher
 
Join Date: Oct 2001
Last Activity: 16 October 2014, 1:04 AM EDT
Location: Southern California
Posts: 332
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #7  
Old 10-14-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 22 October 2014, 9:03 PM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 437 Times in 261 Posts
How about:
nl -ba inputfile | sort -nr | cut -f2-
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help reversing this code new2learn09 Shell Programming and Scripting 3 07-21-2009 10:19 PM
reversing a line kylle345 Shell Programming and Scripting 7 06-03-2009 06:34 AM
reading and reversing a string nikhilneela Shell Programming and Scripting 8 02-24-2009 10:18 AM
awk and reversing scotty_123 Shell Programming and Scripting 1 04-01-2007 02:21 PM
Reversing UID's dreaming1 UNIX for Dummies Questions & Answers 3 03-16-2005 06:36 PM



All times are GMT -4. The time now is 07:01 AM.