The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract column based on name t27 UNIX for Dummies Questions & Answers 3 08-29-2007 01:04 PM
Select Record based on First Column mgirinath Shell Programming and Scripting 8 07-16-2007 01:38 AM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 AM
Select records based on search criteria on first column shashi_kiran_v UNIX for Dummies Questions & Answers 2 12-02-2005 01:49 PM
filter based on column value rraajjiibb Shell Programming and Scripting 2 05-25-2004 09:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-29-2007
user_007 user_007 is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
column based search

Hi,
I need to search one file based on the entry from another;
eg.
file1 has entries;

45654
34768
12345

File2 has entries

23234 somestring 4312
45654 somestring 34768
12345 somestring 45654

so I need to pick first entry in file1 which eg. '45654' and search for this entry only in 'column 1' of File2. Then save the 3rd entry (column 3) from the same line to a variable. (to be used later).
Any help would be appreciated.
  #2 (permalink)  
Old 06-29-2007
craigp84 craigp84 is offline
Registered User
  
 

Join Date: May 2007
Location: Glasgow, Scotland
Posts: 59
Code:
$ ls file?
file1  file2
$ cat file1
45654
34768
12345
$ cat file2
23234 somestring 4312
45654 somestring 34768
12345 somestring 45654
$ join -o 2.3 file1 file2
34768
hope this helps,

-c
  #3 (permalink)  
Old 06-29-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Craig,
Your solution is missing:
45654

Keep in mind that 'join' assumes both files are sorted.

To fix your problem:
Code:
sort file1 > $$temp1
sort file2 > $$temp2
join -o 2.3 $$temp1 $$temp2
PS: I don't think this will resolve 'user_007' issue.
  #4 (permalink)  
Old 06-29-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Here is a solution to put the values into variables:
Code:
sort file1 > $$temp1
sort file2 > $$temp2
join -o 1.1 2.3 $$temp1 $$temp2 | \
while read mLine
do
  set -- $mLine
  echo "Pattern = "$1" Result = "$2
done
rm -f $$*
  #5 (permalink)  
Old 06-29-2007
user_007 user_007 is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
Column based search

Thanks for the responses guys.
But 'joining' isnt going to resolve my problem.

What i am trying to do is to pick one item at a time from file1 and then search the corresponding entry in file2 and save the associated value in column3 of the file 2 in a variable X.

$ cat file1
45654
34768
12345
$ cat file2
23234 somestring 4312
45654 somestring 34768
12345 somestring 45654

So e.g. my first entry in File1 is '45654'.
The corresponding value in Column3 of File2 is 34768.
So i wanted to save this value in a variable X.
  #6 (permalink)  
Old 06-29-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
User_007,
My solution does exactly what you are looking for.
  #7 (permalink)  
Old 06-30-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,416
You can do something like that :

Code:
while read item1
do
   X=$(awk -v value1="$item1" '$1 == value1 { print $3 }' file2)
done < file1
This solution is acceptable only if file1 contains very few records because the file file2 is read for each record of file1.

After having read the value in the variable X, what do you want to do ?
If you don't want to update the file file2, the best way is to use the Shell_Life' solution ( item1=$1 and X=$2 ).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0