for loop - reverse reading


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop - reverse reading
# 1  
Old 02-09-2012
for loop - reverse reading

All,

Here is my for loop
Code:
export CFGLIST="LIST1 LIST2 LIST3"
 
for i in $CFGLIST
do
  echo print $i
done

The output will be
Code:
LIST1
LIST2
LIST3

But i want it display
Code:
LIST3
LIST2
LIST1

Can you please help me how to read in reverse order?

Thanks
Bala

Last edited by Franklin52; 02-09-2012 at 05:33 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-09-2012
Code:
 
a="LIST1 LIST2 LIST3"
$ echo $a | nawk '{for(i=NF;i>=1;i--){print $i}}'
LIST3
LIST2
LIST1

# 3  
Old 02-09-2012
Code:
#!/bin/bash

CFGLIST=(LIST1 LIST2 LIST3)
i=${#CFGLIST[*]}
((i-=1))
while [ $i -ge 0 ]
do
  echo ${CFGLIST[$i]}
  ((i-=1))
done

--ahamed
# 4  
Old 02-09-2012
kamaraj, i'm finding difficulty without nawk - Thanks


Ahamed - if i have as below

CFGLIST=("LIST1 LIST2 LIST3") - It returns same value


Can you please help?

Thanks
Bala

Last edited by baluchen; 02-09-2012 at 08:06 AM..
# 5  
Old 02-09-2012
use awk instead of nawk

---------- Post updated at 05:54 PM ---------- Previous update was at 05:49 PM ----------

Code:
 
$ echo $CFGLIST | perl -lane 'print join(" ",reverse @F)' 
LIST3 LIST2 LIST1

# 6  
Old 02-09-2012
A simple Shell method:

Code:
export CFGLIST="LIST1 LIST2 LIST3"
set ${CFGLIST}
echo "$3"
echo "$2"
echo "$1"

./scriptname
LIST3
LIST2
LIST1

# 7  
Old 02-09-2012
Code:
export CFGLIST="LIST1 LIST2 LIST3"

revlist=
for i in $CFGLIST
do
  revlist="$i $revlist"
done

for i in $revlist
do
  echo "print $i"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading line in while loop

Hello Team, i have to read line by line in a while loop, and the input file has:. # The script will start cppunit test application to run unit tests. -LD_LIBRARY_PATH=$CPPUNIT_HOME/lib:\ +LD_LIBRARY_PATH=$VOBTAG/SS_UnitTest/lib:\ $VOBTAG/SS_BFD/BFDSCLI/build:\ ... (7 Replies)
Discussion started by: chandana hs
7 Replies

2. Shell Programming and Scripting

Reverse the output using for loop

Good morning!! Im trying to ge tthe output in this for loop to be reversed. #!/usr/bin/perl $i = 1; for($i != 0 ; $i < 11 ; $i++){ print "$i\n"; } Ive tried changing the i++ to i--, but it makes the outputted numbers different also. Thanks bigben (4 Replies)
Discussion started by: bigben1220
4 Replies

3. Shell Programming and Scripting

Perl: Reading in reverse.

Is there a means of reading files in reverse? I just want to be able to read a file from the beginning and once I read a particular line, I start reading lines backward from there. Now, I could toss everything into a string array, but the file I'll be reading is roughly 100MB. I don't want... (3 Replies)
Discussion started by: mrwatkin
3 Replies

4. UNIX for Dummies Questions & Answers

Reading inside a loop

Hi I am trying read in side a do statement but it is not working it is just printing abc before read but not stopping in abc for user input Can anybody please help #!/usr/bin/ksh cat sample_file | while read ln_source3 do param=`echo $ln_source3 | nawk... (1 Reply)
Discussion started by: ssuresh1999
1 Replies

5. Shell Programming and Scripting

While loop reading a record

Hi, I am trying to create a while loop that will do the following: INFILE= list of new records that need to be added after last previous record while read record do find the last record processed create list of new records output to a file echo "$record">> $NEWFILE done ... (9 Replies)
Discussion started by: shortyball24
9 Replies

6. Shell Programming and Scripting

while loop not reading last line

hi all, i have a while loop which i am using to read lines into an array: k=0 exec 10<file while read LINE <&10; do ARRAY=$LINE ((k++)) done exec 10>&- echo ${ARRAY} for some reason when i display the array it is not showing the last row in the file. any help appreciated. (1 Reply)
Discussion started by: npatwardhan
1 Replies

7. UNIX for Dummies Questions & Answers

reading more than one variable into a for loop

Hi, I have a file (details.txt) with 3 rows of variables ie... name postcode age john D fr25dd 25 mark W ab122aa 22 phil C cd343bb 33 What I want to do is read down the list with a loop and add each field into a one line piece of text... So I have a file (test1) which reads;... (3 Replies)
Discussion started by: starsky
3 Replies

8. Shell Programming and Scripting

reading from 2 files through while loop

hi i have two files cat input.txt 123456| 43256 456482|5893242 cat data.txt xv 123456 abcd dsk sd 123456 afsfn dd df 43256 asdf ff ss 456482 aa sf 5893242 ff ff aa 5893242 aa aa i need to read inputs from input.txt and find data for data.txt. then i need to print them as a... (2 Replies)
Discussion started by: windows
2 Replies

9. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

10. Shell Programming and Scripting

reading from within the loop --std i/p problem

i have a teat file having data like one 12/3 two 23/09 three 12/12 now from another script i want to read one line at a time ,cut field one and two in two separate variable ,compare field1 with another variable but outside the loop .If i found a match i want to take from user the value... (2 Replies)
Discussion started by: mobydick
2 Replies
Login or Register to Ask a Question