Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOR Loop 1

Status
Not open for further replies.

driddickemc

IS-IT--Management
May 3, 2004
42
US
Have a 2 files..

file1.txt

dddddd
ffffff

file2.txt

dddddd 111 222 333
ffffff 333 444 555


for i in `cat file1.txt`
do
grep -i $i file2.txt
done

returns nothing.. when I ...do echo $i it returns the contents of the cat command..

Please advise..
 
Do you have a email I can send you this to quick and the output.. ?
 
See sequence below..


riddid1@USTGRIDDID1L1C /cygdrive/c/
$ grep -i -f file2.txt file1.txt
dddddddd 444444444444

riddid1@USTGRIDDID1L1C /cygdrive/c/
$ cat file2.txt
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd
riddid1@USTGRIDDID1L1C /cygdrive/c/
$ cat file1.txt
aaaaaaaa 111111111111
bbbbbbbb 222222222222
cccccccc 333333333333
dddddddd 444444444444
riddid1@USTGRIDDID1L1C /cygdrive/c/
$ for -i in `cat file2.txt`
> do
> grep -i $i file1.txt
> done
bash: `-i': not a valid identifier

riddid1@USTGRIDDID1L1C /cygdrive/c/
$ for i in `cat file2.txt`; do grep -i $i file1.txt; done
dddddddd 444444444444

riddid1@USTGRIDDID1L1C /cygdrive/c/
 
Hi

Well, here are my outputs for your sample files :
Code:
[blue]master #[/blue] grep -i -f file2.txt file1.txt
aaaaaaaa 111111111111
bbbbbbbb 222222222222
cccccccc 333333333333
dddddddd 444444444444

[blue]master #[/blue] for i in `cat file2.txt`; do grep -i $i file1.txt; done
aaaaaaaa 111111111111
bbbbbbbb 222222222222
cccccccc 333333333333
dddddddd 444444444444

[blue]master #[/blue] grep --version | head -1
GNU grep 2.5.3
Are you sure there is no mess with DOS type end-of-line marks ?


Feherke.
 
Possibly.. I'm using cygwin.. and notepad..blahhh
 
Ran dos2unix on all files and works great..Thx for your help !
 
Hi

Interesting. I run [tt]unix2dos[/tt] on those two sample files. Even after that [tt]grep[/tt] displayed the correct results. Usually it guesses the end-of-line correctly...

Feherke.
 
$ grep --version |head -1
grep (GNU grep) 2.5.1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top