Foreach issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Foreach issue
# 1  
Old 09-19-2011
Question Foreach issue

Hello,

I found that this foreach should work with two lists (source: Wikipedia.org)
Code:
foreach i {1 2 3} j {a b c} { puts "$i $j"}

==
I try smth. like:
With two text files:
first.part
second.part
Code:
 
foreach first (`cat first.part`) second (`cat second.part`)
toolcommand $first -toolcommand $second
end




TCSH is used.

Also script solutions are welcome, like save line x in first.part and line x in second.part to two vars and use them later...

Thanks
# 2  
Old 09-19-2011
Code:
set valf=`cat first`
@ num=1
foreach vals ( `cat second` )
toolcommand $valf[$num] ; toolcommand $vals
@ num++
end

regards
ygemici
# 3  
Old 09-19-2011
Thanks for the fast response.
Nearly what I wanted.

But this puts the complete file into the first var and prints always the complete first var and not just the related lines.

Quote:
File1:
A
B
C
File 2:
1
2
3
Quote:
Your Output:
A B C1
A B C2
A B C3
^^ Prints A B and C always.

My wanted output
A1
B2
C3
Used code:
Code:
#! /bin/tcsh -f
set valA=( `cat first` )
@ num=1
foreach valB ( `cat second` )
  echo "$valA""$valB"
  @ num++
end

Issue is, that you assigned the complete "cat FileONE" to your var outside of the loop.
This is why i wanted to use these foreach with two lists. But other working solutions are also welcome.

Thanks
# 4  
Old 09-19-2011
Code:
awk 'NR==FNR{a[++i]=$0;next}{b[++j]=$0}END{for(k=1;k<=j;k++){print a[k]b[k] }}' file1 file2

--ahamed

---------- Post updated at 07:17 AM ---------- Previous update was at 07:17 AM ----------

paste will create a space
Code:
paste  file1 file2

--ahamed
# 5  
Old 09-20-2011
Quote:
Originally Posted by ahamed101
Code:
awk 'NR==FNR{a[++i]=$0;next}{b[++j]=$0}END{for(k=1;k<=j;k++){print a[k]b[k] }}' file1 file2

--ahamed

---------- Post updated at 07:17 AM ---------- Previous update was at 07:17 AM ----------

paste will create a space
Code:
paste  file1 file2

--ahamed
Thanks,
can you show me how i can use now this in a script with usage of the vars? (nawk BEGIN usage?)

I want to use each element for my commands.

so for example like:
Code:
 
echo This is first line from file1 "varA[$1]" and this is the corresponding line from file2 "varB[$2]"

In your example I want to use in my script these vars: a[k]b[k]
Just don't know how to access them in the script (how to run your awk command there)

---------- Post updated 20th Sep 2011 at 02:44 PM ---------- Previous update was 19th Sep 2011 at 04:32 PM ----------

Still not able to get the wanted result... hanging still on the print issue...

With paste and without does not print out what I want...
with paste I do not see any other printouts as the files
Code:
#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
      print COMMAND
      print a[k]
      print ("COMMAND_OPTION")
      print b[k]
    }
}' | paste file1 file2

^^ i see no COMMAND and neither COMMAND_OPTION ^^
Tried with one print and multiple
Tried with one printf and usage of both %s
with double quotes and single quotes - with brakets and w/o...
don't know why I do not see my wanted printout.

i want to get:
Quote:
COMMAND line1_form_file1 COMMAND_OPTION line1_from_file2
COMMAND line2_form_file1 COMMAND_OPTION line2_from_file2
COMMAND line3_form_file1 COMMAND_OPTION line3_from_file2
Thanks

---------- Post updated at 03:02 PM ---------- Previous update was at 02:44 PM ----------

Code:
#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
      print "COMMAND", a[k], "COMMANDOPTION", b[k]
    }
}' file1 file2

Quote:
OUTPUT:
COMMAND COMMANDOPTION line1_form_file1
COMMAND COMMANDOPTION line2_form_file1
COMMAND COMMANDOPTION line3_form_file1
COMMAND COMMANDOPTION line1_from_file2
COMMAND COMMANDOPTION line2_from_file2
COMMAND COMMANDOPTION line3_from_file2

please help... cant get the wanted output:
Quote:
COMMAND line1_form_file1 COMMAND_OPTION line1_from_file2
COMMAND line2_form_file1 COMMAND_OPTION line2_from_file2
COMMAND line3_form_file1 COMMAND_OPTION line3_from_file2
---------- Post updated at 03:07 PM ---------- Previous update was at 03:02 PM ----------

Quote:
Originally Posted by ahamed101
Code:
awk 'NR==FNR{a[++i]=$0;next}{b[++j]=$0}END{for(k=1;k<=j;k++){print a[k]b[k] }}' file1 file2

--ahamed

---------- Post updated at 07:17 AM ---------- Previous update was at 07:17 AM ----------

paste will create a space
Code:
paste  file1 file2

--ahamed
^^ The print a[k]b[k] } does not work

a[k] is EMPTY
b[k] contains the information from both files!
# 6  
Old 09-20-2011
Code:
#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {      
       print "COMMAND " a[k] "COMMAND_OPTION " b[k]    
    }
}' file1 file2

--ahamed
# 7  
Old 09-20-2011
Quote:
Originally Posted by ahamed101
Code:
#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {      
       print "COMMAND " a[k] "COMMAND_OPTION " b[k]    
    }
}' file1 file2

--ahamed

^^ I still tried this... output is:
Quote:
COMMAND COMMAND_OPTION line1_form_file1
COMMAND COMMAND_OPTION line2_form_file1
COMMAND COMMAND_OPTION line3_form_file1
COMMAND COMMAND_OPTION line1_from_file2
COMMAND COMMAND_OPTION line2_from_file2
COMMAND COMMAND_OPTION line3_from_file2
any hints? :-/
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ssh2 foreach loop issue

Hello Everyone, I have the following codes that only works for the first login element. I can't get it work with the second and third login credentials. Can anyone here see the problem? I ran the code with host1, it works as expect. see below: # ./test.pl host1 Trying to connect... (3 Replies)
Discussion started by: tqlam
3 Replies

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

3. Shell Programming and Scripting

foreach in csh

I have a foreach loop in a csh script and I noticed that it tries to find the files with the pattern *$searchpt* in the file name. I'm confused as I never specified checking for the files. foreach f ( *$searchpt* ) set fnew = `echo $f | awk -v searchpat=$searchpt \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

expect ssh script issue with if and foreach

Hi, I am trying to create an ssh script to login to cisco routers and activate/deactivate bgp neighbors if they match certain conditions. I dont think my "if" and "foreach" are working correctly. Any help is appreciated. Below is my script: ... (0 Replies)
Discussion started by: blahblahsomeone
0 Replies

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

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

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 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