Assigning values to 2 variables within for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning values to 2 variables within for loop
# 1  
Old 06-07-2013
Assigning values to 2 variables within for loop

Hi All,

Is it possible to grep for two files and assign their names to two separate variables with for loop? I am doing the below currently:

Code:
if [ $FLAG = 0 ]
   then
        for fname in $( cd $dirA ; ls -tr | grep "^Ucountry_file$")
        do
              InFile=$dirA/$fname
              OutFile=$dirB/$fname.processed
                        :
                        :
                        :
               more lines of code
        done
fi

fname in the for loop here holds the name of the Ucountry_file. Likewise, in the same $dirA, there is another file called Rcountry_file. I want to grep for that as well and assign to another variable using the same for loop. Can it be done?

Thanks in advance.
# 2  
Old 06-07-2013
I think you want to use egrep to extend the grep capability. You could:-
Code:
.... egrep "^Ucountry_file$|^Rcountry_file$"
.... egrep "^[UR]country_file$"

Have I got this right, or have I missed the point?

The first option specifies the name you are after with a | to make it an 'or' comparison. The second option searches for a line starting with either U or R, following by country_file then the end of the line.



I hope that this helps, but let me know if I'm miles off track. I must admit I don't know where $dirB comes from in your code.



Robin
Liverpool/Blackburn
UK
# 3  
Old 06-07-2013
In general, instead of cramming thousands of filenames in backticks into a for, you should be doing command | command | while read because for has some problems here, like a limit on number of files, and potential accidental splitting on spaces instead of lines.

Anyway. You can do two greps separately, saving one into a file, then opening the file to read later...

Code:
command1 > /tmp/$$

exec 5</tmp/$$ # Open file into FD 5
rm /tmp/$$ # Delete file (Will stay readable until you close it )

command2 | while read LINE1
do
        read LINE2 <&5

        echo "LINE1 $LINE1 LINE2 $LINE2"
done

exec 5<&- # Close file

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 06-07-2013
Why all that ls, cd, grep at all? Try
Code:
for InFile in "$dirA/[RU]country_file"; do OutFile="$InFile".processed;...;done


Last edited by RudiC; 06-07-2013 at 04:31 PM.. Reason: put double quotes around $InFile
# 5  
Old 06-07-2013
I must be missing something basic here. I'll go a small step beyond where RudiC went. I don't see the need for a for loop at all. If I'm reading the code correctly the commands in the for loop will be executed once if $dirA/Ucountry_file exists and will not be executed if that file does not exist. If I understand the question correctly, I think the following would be a much simpler way to handle it:
Code:
ft=country_file
fname=U$ft
if [ $FLAG = 0 ]
then
        if [ -e "$dirA/R$ft ] && [ -e "$dirA/$fname" ]
        then
                InFile=$dirA/$fname
                InFile2=$dirA/R$ft
                OutFile=$dirB/$fname.processed
                        :
                        :
                        :
                more lines of code
        else    ... do ... if Rcountry_file or Ucountry_file is missing ...
        fi
fi

In the then part of the inner if, $fname will be what it was in the original for loop, $InFile will be as it was in the original for loop (a pathname for the Ucountry_file in $dirA) and $InFile2 will be a pathname for the Rcountry_file in $dirA.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

2. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

3. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

4. Shell Programming and Scripting

Assigning fields values to different variables

Hi, I have this code: cat file.txt | awk -F, 'NR==1{print $6","$8","$10","$20","$21","$19}' > file.tmp VAR1=`cat file.tmp | cut -d "," -f1` VAR2=`cat file.tmp | cut -d "," -f2` VAR3=`cat file.tmp | cut -d "," -f3`; VAR4=`cat file.tmp | cut -d "," -f4`; VAR5=`cat... (1 Reply)
Discussion started by: Tr0cken
1 Replies

5. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

6. Shell Programming and Scripting

Need help in assigning output of n commands to n variables automatically inside a for loop

Please help me to automatically assign the output of awk command to the variables cs3, cs4, cs5 and cs6 using a for loop. The below code is not working. for i in 3 4 5 6 do cs$i=`awk -F"|" 'BEGIN{sum=0}{sum=sum+$'$i'}END{printf("%d\n", sum)}' css` done echo $cs3 $cs4 $cs5 $cs6 (9 Replies)
Discussion started by: thulasidharan2k
9 Replies

7. Shell Programming and Scripting

Assigning values to reference variables for a dynamic menu driven script.

How do I assign values to reference variables? I am assigning a variable name to --> $user_var Then I am trying to change its underlying variable value by $((user_var))=$user_value .. its failing,, Please let me know if there is a way to do this dynamically.. FileA.props... (5 Replies)
Discussion started by: kchinnam
5 Replies

8. Shell Programming and Scripting

retreiving and assigning values and manipulating string in a for loop

Hi I am new to shell scripting and i am preparing a script. for now i am work on a sub part of it..but i am unable to make it work. --- the test code that i am working on -------------------------- IFS="" Sample_eve=`psg proc_s | grep tY` n=0 for line in $Sample_eve... (41 Replies)
Discussion started by: Anteus
41 Replies

9. 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

10. Shell Programming and Scripting

Problem in assigning values to variables

Hi, I have some problem in assigning values to variables: This is what Iam literally doing: i=0 input=test temp$i = $input In the sense, I try to assign the value in the variable input (ie., test) to another variable temp0 (since i is assigned 0, temp$i is temp0). Seems simple, but I get... (3 Replies)
Discussion started by: mohanprabu
3 Replies
Login or Register to Ask a Question