Dealing with Double Loops, Arrays and GREP


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dealing with Double Loops, Arrays and GREP
# 1  
Old 05-28-2012
Question Dealing with Double Loops, Arrays and GREP

Can someone please help me to learn how to deal with loops, arrays and grep?
I have two arrays (lets say I and j) each in a separate file
Quote:
Ruby, Kate, Askley, John ....
Smith, Brown, Green, Casey, ...
And have file with lines of data I need to extract, such as
Ruby Smith: some text here
Ruby Smith: some other text here
Ruby Brown: some text here
Ruby Green: some text here
And I need each of these names into one new file; so each name category will have its own file
Now sure what I think is the right thing and I don't know the syntax... so would appreciate your help
Code:
while ( i =1 and i<n){
	while (j= 1 and i<n){
		grep “i.*j” maindatafile.txt > $1file.txt
		j ++
	}
	i ++
}

Would something like this even work? How is it going to know what is my “I” and “J” when they are in my files? Should I give a file name?
and I wonder whether the $1file will increase as well? or shall I write something to add that as well?

Thank you in advance for your time
A-V
# 2  
Old 05-28-2012
What's your system?

What's your shell?
# 3  
Old 05-28-2012
I am using Cygwin on windows ... so more command like but I have Perl as well

I wrote what was in my head down as a code so I am sure that would not work with unix/cygwin
# 4  
Old 05-28-2012
cygwin is a system that lets you install what programs you want.

What's your shell? I'm assuming BASH until you say otherwise...

Code:
for ((I=1; I<N; I++))
do
        for ((J=1; J<N; J++))
        do
                grep "$i.*$j" maindatafile
        done > outputfile$I
done

I don't think that 'grep' does what you think it does, but you haven't told me what you're trying to match either.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-29-2012
I think i am using shell as well

I want to match one name from names list with a family name on the family name list
but some of the lines on my maindatafile have a middle name; like Ruby Anne Smith
I want anything starting with Ruby and ending in Smith in one file.

is grep what I need to use for that?

---------- Post updated at 12:01 PM ---------- Previous update was at 11:54 AM ----------

Thank you very much for sending me the right syntax of the code

I have some question

Do I have to define I and J before that code?
or is there a way I can tell it to read from two files?

---------- Post updated 05-29-12 at 08:58 AM ---------- Previous update was 05-28-12 at 12:01 PM ----------

i have tested the code with my data and there seems to be a problem

my code now looks like this

Code:
ARRAY=(ruby kate john)
ARRAY2=(smith brown green)
ELEMENTS=${#ARRAY[@]}
ELEMENTS2=${#ARRAY2[@]}
for (( i=0;i<$ELEMENTS;i++)); do
	for (( j=0;j<$ELEMENTS2;j++)); do
    		grep “${ARRAY[${i}]}.*${ARRAY2[${j}]}” test.txt 
       done > output$1.txt
done

i dont know y i only get one output and that is for the last element in ARRAY and i get it combined, so I get an output of all in one file

john smith ....
john brown ...
john green ...

so it seems to be over written but then again each should be in a separate file

Quote:
output x = john smith
output y = john brown
output z = john green
can you please tell me where am I going wrong>?
# 6  
Old 05-29-2012
First off, writing your code in MS Word has absolutely destroyed it, replacing the usual characters with malicious 'smart quotes'. Stop doing that.

Second off, you don't need arrays.

Third off, grep can match lists of names by itself.

Code:
for X in ruby kate john
do
        egrep "$X (smith|brown|green)" filename > $X.txt
done

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 05-29-2012
Thanks for your replay

Oooopsss.... i didnt know that about MS word... learned one more thing Smilie

you are right, this does work and dont need arrays but

people with the same fist name and last name should do to one file; not people with same first name only.
output file 1 ---- ruby smith
output file 2 ---- ruby brown
output file 3 ---- ruby green

when I used the egrep the way mentioned but it is not taking out the lines with people who have a middle name for example
Quote:
ruby anne smith
john thomas brown
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For i in loops on 2 arrays

Hey , i have this script and i have these loops so it can find a match between 2 arrays : ARRAY_1=(one two three) ARRAY_2=(A B C) VAR='B' for NUMBERS in "${ARRAY_1}" do for LETTERS in "${ARRAY_2}" do if ];then VAR='LETTERS' ... (2 Replies)
Discussion started by: batchenr
2 Replies

2. Shell Programming and Scripting

Using grep with multiple loops in reading files

I am trying to read a file line by line and then search that line in another file and get a particular column from the second file. I have written this code.but its not working properly #!/bin/sh while read keyword in duplicate.txt do echo $keyword while read line do ... (7 Replies)
Discussion started by: Prachi Gupta
7 Replies

3. UNIX for Dummies Questions & Answers

Dealing with Empty files, AWK and Loops

I write this bit of code to calculate the mean and variance for all the files in one directory and print the mean and variance in a separate folder but with the same file name. FILES="data/*" for X in $FILES do name=$(basename $X) awk '{x=$0; s+=$0; n++} END{mean=s/n; for (i in x){ss... (20 Replies)
Discussion started by: A-V
20 Replies

4. Shell Programming and Scripting

GREP/CUT/AWK to Arrays

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: Code: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa... (1 Reply)
Discussion started by: gc_sw
1 Replies

5. Shell Programming and Scripting

Korn Shell programming (FTP, LOOPS, GREP)

Hello All, I have another Korn shell question. I am writing a script that will ftp a file from my production database to my test database. To this, I have to construct a loop that checks a specified folder for a file. If the file exists, I want it execute the ftp protocol and then exit. ... (2 Replies)
Discussion started by: jonesdk5
2 Replies

6. Shell Programming and Scripting

conflict: "for" loops and double precision numbers

Hi folk, Hope you enjoy the summer. I am stock after one day working, the problems are the following: 1) I want to write a for loop in a shell script code that has a double precision step as: #!/bin/bash START=0.00001 STOP=0.001 for((i = START ; i< STOP ; i=2*i)) do echo "$i"... (1 Reply)
Discussion started by: habib
1 Replies

7. Shell Programming and Scripting

arrays and while loops in bash

hi guys, i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array.. i get a =: command not found error.. i=0 while do ${master_array}=${ARRAY} ((i++)) done is there something i am missing? (4 Replies)
Discussion started by: npatwardhan
4 Replies

8. Shell Programming and Scripting

trying to learn for loops, and arrays at the same time

Ok, I've already completed the task this is for, but now I'm trying to go back and find more eloquent solutions for future reference. I have a report I've generated that is formatted like this: 1033 1 1079 4 1453 5 2205 6 1933 7 461 8 646 9 1655 12 975 13 1289 14 The first number is... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

9. Shell Programming and Scripting

korn shell "loops & arrays"

Hi, I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares. For example: file1 has a list of user ids (about 900) from the company's e-mail server. file2 has a list of user ids (about 50 or so) from... (7 Replies)
Discussion started by: muzica
7 Replies
Login or Register to Ask a Question