How to run a loop for assigning strings which are present in a file to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to run a loop for assigning strings which are present in a file to an array
# 1  
Old 03-01-2009
How to run a loop for assigning strings which are present in a file to an array

Hi Forum,

I am struggling with the for loop in shell script.
Let me explain what is needed in the script.
I have a file which will conatin some strings like

file1
place1
place2
place3
checkpoint
some other text
some more text


Now what my requirement is
the words
place1,place2 and place3 are directory names
from this file i have to store these directory names in some array
so first i have to split the file in such a way that i have only directory names that is done with the below line of code:


sed -n -e '/checkpoint/q' -e '1,/checkpoint/p' /tmp/file2
now my file contains only
place1
place2
place3

now my question is how to run a loop so that all these strings are stored in some variables and then inside the loop i can enter into each of these directiries and do a long listing.The long listing part i know,but my only problem is how to enter into each of the directory specified in file2 using a loop.

Hope i have cleary mentioned the problem,appreciate a positive response if my problem is not clear please do let me know.Thanks
# 2  
Old 03-01-2009
If I understand, try this:
Code:
#! /usr/bin/ksh
sed -n -e '/checkpoint/q' -e '1,/checkpoint/p' /tmp/file2 | while read dir ; do
      cd $dir
      ls -l
      cd ..
done
exit 0

This assumes you are positioned in the parent of these directories when you start.
# 3  
Old 03-01-2009
Hey thanks perderabo,
just one doubt..actually i am running a script to generate the above file.

it is generic or dynamic amd i am running it from some other location.
And i am not positioned at the parent location.

Now my question is how to run the above command from some other location
what changes are needed in the above line if i am executing this from other location?

For Eg:
i am running this script from /tmp

and the out put is like this


place1
place2
place3
checkpoint
some other text
some more text


now
place1,place2,place3

are directories under the parent directory /logs
Can you modify the above script so the inside the loop it will go into
/logs/$dir
and then do ls -l??

Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Putting strings into positioning array in loop

i need to add 2 string variables into a positioning array , repeatedly - in loop. First string in $2, second to $3 then up to the desired count incrementing the "position". Using set -- alone does not increment the count so I end up with 2 variables in the array. How do I increment the... (7 Replies)
Discussion started by: annacreek
7 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

4. Shell Programming and Scripting

Return error if - or certain characters are present in a list of strings

I have a list of strings, for example: set strLst = "file1 file2 file3 file4" I want to log an error if some of the fields happen to begin with -, or have characters like ; : ' , ? ] { = Which means for example setting set ierr = 1 (2 Replies)
Discussion started by: kristinu
2 Replies

5. Shell Programming and Scripting

Search strings from array in second file

I have a file search_strings.txt filled with search strings which have a blank in between and look like this: S. g. Erh. o. J. v. d. Chijs g. Ehr.I would like to search the strings in the second given Textfile.txt and it shall return the column number. Can anybody help with the correct... (3 Replies)
Discussion started by: sdf
3 Replies

6. Shell Programming and Scripting

Delete Strings that are present in another file

HI, if a String is present in file1.txt, i want to delete that String from file2.txt. How can i do this?? I am sure that the file1.txt is a subset of file2.txt. (2 Replies)
Discussion started by: jathin12
2 Replies

7. Shell Programming and Scripting

FTP and run a loop for array problem

Hello, I have a problem with my script whereby it does not want to loop. The function of my script is to FTP into a server and go to each directory/volume in the array 'VOL'. The way the loop is suppose to work, is to go into the first volume, get the files of R(yesterday's date) and... (3 Replies)
Discussion started by: tuffgong2008
3 Replies

8. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

9. Shell Programming and Scripting

Another 10 second question. Assigning 2 strings to one string.

Hi, I am trying to combine 2 strings into one new string. I know there are existing threads on this topic, but I am having troubles. The variables have variables within their names which is causing me problems. Bad subsitution is the error. The problem line is in red below. thanks (again) to... (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question