|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Code:
awk -F\| '{printf "%6s\n",$1}' inputfile.txt |
sort -r |
while read i
do
echo "Now processing $i ..."
doneawk 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
|
||||
|
||||
|
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
|
||||
|
||||
|
How about:
nl -ba inputfile | sort -nr | cut -f2- |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|