LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-30-2016, 05:58 AM   #16
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Original Poster
Rep: Reputation: Disabled

@pan64
Quote:
Is this what you are looking for?
Since some of the keywords are common in both setA and setB, I don't think this approach will work. e.g. setA could choose from { Mary|John, Left|Right, Eye|Ear} and setB could choose from{ John|Joseph, Left|Right, Hand|foot}

Last edited by samasat; 11-30-2016 at 05:59 AM.
 
Old 12-01-2016, 12:16 AM   #17
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Original Poster
Rep: Reputation: Disabled
@pan64
Quote:
you only need to remove the files from the list where both keyA and keyB can be found.

Is this what you are looking for?
I don't think this will work since some of the words are common in both the sets. e.g. setA belongs to {John|Mary, Right|Left, Eye|Ear} and setB belongs to {Mary|Amit, Right|Left, Hand|Foot}
If above approach is followed setA=[John,Right,Eye] will never form a pair with setB=[Amit,Right,Hand]
Also, I don't see how pairs would have been formed by the approach you suggested.
 
Old 12-01-2016, 04:22 AM   #18
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,129

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
so you need to tweak that grep in the middle. Probably and awk/perl/python would be better to filter (select the required lines) and also you may mark them (in a second column for example).
Actually you will have a lot of pairs, because all of the lines of keyA1 & keyA2 will be paired to all the lines of keyB1 and keyB2 below that line (I hope you understand what I wanted to explain)
 
Old 12-02-2016, 10:44 AM   #19
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Original Poster
Rep: Reputation: Disabled
Smile

So here is the final working code for me which is almost similar to what MadeInGermany said earlier.

Code:
opFile="FilePairs.txt"
rm -rf $opFile

find . -name "*txt" -path "*keyA1/*" -path "*keyA2/*" > tmpfileA.txt
find . -name "*txt" -path "*keyB1/*" -path "*keyB2/*" > tmpfileB.txt

while read -r fileA
do
     while read -r fileB
     do     
         if [[ "$fileA" < "$fileB" ]] ; then
            printf '%s %s \n' "$fileA" "$fileB"
         fi
     done < tmpfileB.txt >> $opFile;
done < tmpfileA.txt
Difference compared to my old approach is that:
1) The find command output was saved separately
2) While loop read in Lines from there
3) Important - earlier I used to run bash script as a command and I used to redirect terminal output to the intended output file like this:
Code:
>> ./myScript.sh > FilePairs.txt
Now as suggested by MadeInGermany the redirection is directly happening from the end of the inner while loop.

NOTE - the blank spaces in the if statement condition [[ "$fileA" < "$fileB" ]] are important

Thanks everyone
 
Old 12-02-2016, 12:00 PM   #20
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,847

Rep: Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223Reputation: 1223
If you do not print anything in the outer loop then you can move the $opFile redirection to the outer loop.
And if you overwrite it with > then you don't need to rm it.
Code:
opFile="FilePairs.txt"

find . -name "*txt" -path "*keyA1/*" -path "*keyA2/*" > tmpfileA.txt
find . -name "*txt" -path "*keyB1/*" -path "*keyB2/*" > tmpfileB.txt

while read -r fileA
do
     while read -r fileB
     do     
         if [[ "$fileA" < "$fileB" ]] ; then
            printf '%s %s \n' "$fileA" "$fileB"
         fi
     done < tmpfileB.txt
done < tmpfileA.txt > $opFile
 
1 members found this post helpful.
Old 12-05-2016, 11:57 PM   #21
samasat
LQ Newbie
 
Registered: Nov 2011
Location: Bangalore
Distribution: Ubuntu/Cygwin
Posts: 29

Original Poster
Rep: Reputation: Disabled
Thank you MadeInGermany. I appreciate all your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested while loop - similar strings tlegend33 Linux - Newbie 4 09-17-2014 06:04 PM
[SOLVED] infinite nested while loop wolverene13 Programming 3 11-14-2012 08:32 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] for loop and nested find kez1985 Linux - Newbie 1 10-01-2010 10:46 AM
Nested-double loop error Harry Seldon Programming 3 05-06-2006 05:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration