foreach output problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach output problem
# 1  
Old 09-23-2010
foreach output problem

Hi all,

In my file config_recipient:
group1 email1
group2 email2

$class file :
group1
group2

foreach file (`cat $app_dir/config_recipient ' ` )
if ( $class == $file ) then
echo " $file "
endif
end

output i get is :
group1
group2

while the output that i want get is :
email1
email2

anybody can help me?

---------- Post updated at 11:57 AM ---------- Previous update was at 11:38 AM ----------

Hi all,

In my file config_recipient:
group1 email1
group2 email2

$class file :
group1
group2

Code:
foreach file (`cat $app_dir/config_recipient ' ` )
if ( $class == $file ) then
echo " $file "
endif
end

output i get is :
group1
group2

while the output that i want get is :
email1
email2

anybody can help me?
Edit/Delete Message
# 2  
Old 09-23-2010
Try this,
Code:
cat config_recipient
group1 email1
group2 email2

cat class
group1
group2

awk 'NR==FNR{a[$1]=++i;next} { if (a[$1]) { print $2 }}' class config_recipient

# 3  
Old 09-23-2010
Hi ,
i still cannot get wht u mean,
Code:
 foreach file (`cat $app_dir/config_recipient | awk {print $1,$2 } ` )
            echo " $file "
  end

The output for this code :
group1 email1
group2 email2

However if i write like this :
Code:
 foreach file (`cat $app_dir/config_recipient | awk '{print $1,$2}' ` )
               if ( $class == $file ) then
                echo " $file "
               endif
  end

which mean if group1 == group1($class== $file), the output should be like this:
Code:
email1

But i get the output for run that sript :
Code:
group1

Anybody can help me solve it?

---------- Post updated at 02:20 PM ---------- Previous update was at 01:56 PM ----------

Can any 1 know how to solve it?
or use other loop statement?

---------- Post updated at 05:18 PM ---------- Previous update was at 02:20 PM ----------

Hi pravin27 or any1,
know how to solve it?
# 4  
Old 09-23-2010
Have you tried the code which I posted ?
What value is in variable $class ?
# 5  
Old 09-23-2010
erm..try alreay cannot run la...
# 6  
Old 09-23-2010
What error are you receiving ?
# 7  
Old 09-23-2010
Hi pravin,
Actually my script is not run like ur 1 mention.
I actually use while loop to assign the value as variable in my config_recipient file.
And now i got another file name folder. Actuallty this file also must assign value inside it as variable.
While the problem is both of file must link together and i cannot run 2 while loop for both of file at 1 time...so i try to loop 2nd file use awk method.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

foreach loop problem

Dear all, I wrote a script to download files and move files in directories according to their name. Now here is the problem: Both p101 and p360 data download successfully, but when I move them according to the year and month, only p101 data can be placed at the right location, p360,... (1 Reply)
Discussion started by: handsonzhao
1 Replies

2. Shell Programming and Scripting

Foreach issue

Hello, I found that this foreach should work with two lists (source: Wikipedia.org) foreach i {1 2 3} j {a b c} { puts "$i $j"} == I try smth. like: With two text files: first.part second.part foreach first (`cat first.part`) second (`cat second.part`) toolcommand $first... (22 Replies)
Discussion started by: unknown7
22 Replies

3. Shell Programming and Scripting

Using foreach with two lists

Hi everybody, I'm trying to use a foreach command with two lists. The file.txt looks like this: var1: 100 200 300 var2: 3 6 9 I'm trying to use a foreach command to associate the two variables together. My script looks like this: #! /bin/tcsh set a=(`cat file.txt | grep 'var1' | cut -d... (8 Replies)
Discussion started by: SimonWhite
8 Replies

4. UNIX for Dummies Questions & Answers

foreach question

OK, so I am extremely rusty and am just getting back to Unix after 9 years. I'm stuck on something easy. I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. This works fine to do the search: while read i; do grep $i file2; done... (3 Replies)
Discussion started by: moldoverb
3 Replies

5. UNIX for Advanced & Expert Users

Problem with foreach loop

Hi All, Is there any problem with the below 'foreach' loop? foreach risk_factor ($(cat "$rf_list")) where "rf_list=$SCRIPT/Utility/rflist.txt " I'm wondering, it is throwing below error message: syntax error at line 34: `(' unexpected Any idea/suggestions ? Thanks in advance /... (7 Replies)
Discussion started by: ganapati
7 Replies

6. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

7. Shell Programming and Scripting

Foreach loop

What am I doing wrong with this foreach loop? foreach var ($argv) @sum = $sum + $var (4 Replies)
Discussion started by: haze21
4 Replies

8. Shell Programming and Scripting

foreach folder

Hi, I'm having a small issue here and I can't get it to work. I'm programming a script for bash and I need to do something to all the folder in a directory. So I'm in the directory and I want to use the foreach statement but I dont know how to reference all the folders of that directory. To make... (7 Replies)
Discussion started by: eltinator
7 Replies

9. Shell Programming and Scripting

foreach/grep help!

#!/bin/bash foreach x (67402996 67402998) { grep -a x FINAL2006.dat >> MISSING_RECORDS.dat } I'm trying to pass a list to the variable x, and then grep for that string in FINAL2006.dat... Final2006.dat is in the same folder as my .sh file. I call this with a .cmd file... At any rate,... (6 Replies)
Discussion started by: JimWork
6 Replies
Login or Register to Ask a Question