foreach/grep help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting foreach/grep help!
# 1  
Old 07-27-2007
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, it doesn't seem to be working. Any thoughts?

Thanks!

jim
# 2  
Old 07-27-2007
Jim,
Duplicate threads are not allowed here.

Please remove one of them:
https://www.unix.com/unix-for-dummies...grep-help.html
# 3  
Old 07-27-2007
Jim,
The syntax you are using is very similar to 'perl'.

The 'foreach' is a command from 'csh'.:
Code:
#!/bin/csh
foreach x (67402996 67402998)
 grep -a $x FINAL2006.dat >> MISSING_RECORDS.dat
end

# 4  
Old 07-27-2007
Thanks!

I have a new problem: my Cygwin install doesn't appear to have csh.exe in bin... How do I get that?
# 5  
Old 07-27-2007
See if you have 'ksh':
Code:
#!/bin/ksh
for x in 67402996 67402998
do
  echo 'x = '$x
done

# 6  
Old 07-27-2007
#!/bin/tcsh
foreach x (67402996 67402998)
grep -a $x FINAL2006.dat >> MISSING_RECORDS.dat
end

This *almost* works, but it only processes the first number (correctly! Woot!) and then stops.

Any thoughts?

jim
# 7  
Old 07-27-2007
Your looping is working properly, try the following:
Code:
#!/bin/tcsh
foreach x (67402996 67402998)
  echo 'x = '$x
end

Then your problem must be in the 'grep' statement.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

Learning foreach

im newbie at shell scripting. why do the following code #!/bin/tcsh setenv CBC ~/cbc/models/ foreach mix (p00p00 p02p00 p02p04) echo $mix cp $CBC/*$mix*Gyr*fits $mix/ end print(copy) only the first mix? % ./copyfromcbc.sh p00p00 wasn't it supposed to run through all words... (0 Replies)
Discussion started by: prtc
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. UNIX for Dummies Questions & Answers

foreach command ?!

SaLAam What is the best way to change a word withing a files name. I know I'm not clear enough I will give example : - I have in /test/test N number of files like this 1662_WAITING 1666_WAITING 1670_DONE 1678_DONE 1663_WAITING 1667_WAITING 1673_WAITING ... (5 Replies)
Discussion started by: geoquest
5 Replies
Login or Register to Ask a Question