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
search fields within a file Pablo_beezo Shell Programming and Scripting 4 10-20-2008 10:52 AM
Search for String within File and Return File Name bggibson UNIX for Dummies Questions & Answers 4 09-25-2008 05:45 AM
How to search for two fields jisha Shell Programming and Scripting 3 04-23-2008 05:16 AM
search and replace different fields tungaw2004 UNIX for Dummies Questions & Answers 3 03-29-2007 03:16 AM
Shell script to return all the ID's from file based on the distribution ID search kumbhatalok UNIX for Dummies Questions & Answers 1 10-06-2006 12:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-17-2008
da2357 da2357 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
Search flat file and return 3 fields

I need to be able to search a flat file (comma-separated values) for a specific value and then return the following 2 fields into variables. Here's a sample flat file:

SN,Account,IPaddress
W120394YF,adam,10.0.20.2
W394830PR,betty,10.0.20.3
W847582TD,charlie,10.0.20.4
W749509AY,donna,10.0.20.5

I already have BASH code that extracts the computer's serial number and stores it to SERIALNUM. I want to search the file for that SERIALNUM and return the following 2 fields in ACCT and IPADD. Suggestions?
  #2 (permalink)  
Old 11-17-2008
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
  
 

Join Date: Oct 2007
Location: East Coast
Posts: 58
Using awk like this may help point you in the right direction:

serialnum=`awk -F"," '/W394830PR/{print $1}' file`
account=`awk -F"," '/W394830PR/{print $2}' file`
ipaddress=`awk -F"," '/W394830PR/{print $3}' file`
  #3 (permalink)  
Old 11-18-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

i would suggest to first read in the whole file in three array, one for the serial, one for the user and one for the ipadd.

Code:
declare -a SERIAL ACCT IPADD 
let count=0 
while IFS=, read a b c 
do 
    SERIAL[$count]=$a; ACCT[$count]=$b; IPADD[$count]=$c; ((count++)) 
done < file
This sets the internal field separator to "," and reads the three comma separate fields into the three variables which are pushed into the declared arrays.

Now you can search this array like this:

Code:
let i=0 
while [[ $i -lt ${#SERIAL[@]} ]] 
do 
  [[ ${SERIAL[${i}]} = W120* ]] && printf "%s %s\n" ${ACCT[${i}]} ${IPADD[${i}]} 
  ((i++)) 
done
This loops trough the array. ${#SERIAL[@]} gives you the number of entries of an array. If a certain SERIAL is found, the corresponding data are output.

HTH Chris
  #4 (permalink)  
Old 11-18-2008
yongitz yongitz is offline
Registered User
  
 

Join Date: Apr 2008
Location: Philippines
Posts: 68
Code:
awk -F"," -v pat=$SERIALNUM '$0 ~ pat {print $2,$3}' your_flat_file.txt
This assumes that you only got one serial number stored in SERIALNUM variable.
  #5 (permalink)  
Old 11-18-2008
da2357 da2357 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
Quote:
Originally Posted by in2nix4life View Post
Using awk like this may help point you in the right direction:

serialnum=`awk -F"," '/W394830PR/{print $1}' file`
account=`awk -F"," '/W394830PR/{print $2}' file`
ipaddress=`awk -F"," '/W394830PR/{print $3}' file`
Great (and thanks), this got me 99% there. Here's a snippet of what I have, using your idea:

Code:
#!/bin/bash
USERLIST="/Users/johndoe/.bin/xBackup_users"
SRCHFOR="W8735UL8Z5V"
#
USERNAME=`awk -F "," '/W8735UL8Z5V/{print $2}' $USERLIST`
PASSWORD=`awk -F "," '/W8735UL8Z5V/{print $3}' $USERLIST`
RSYNCIP=`awk -F "," '/W8735UL8Z5V/{print $4}' $USERLIST`
#
clear
echo
echo Your account name is $USERNAME
echo Your password is $PASSWORD
echo Your IP address is $RSYNCIP
echo
exit
To finish this, I'd rather use the variable SRCHFOR in the awk command and I've tried it several ways, but aren't sure how to get it to work. (I already separate code that extracts the serial number from the machine.)

Any final ideas?
  #6 (permalink)  
Old 11-18-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink

Take a look at previous suggestion (of yongitz):
Quote:
awk -F"," -v pat=$SERIALNUM '$0 ~ pat {print $2,$3}' your_flat_file.txt
You need to specify a variable for use in awk, like done above. The -v says to set a variable, and the one assigned is pat, and it is equal to $SERIALNUM. You would need to do the same for your SRCHFOR variable. Then, you could use pat (or any other variable name you assign) within awk to do your matching.
  #7 (permalink)  
Old 11-18-2008
da2357 da2357 is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
Quote:
Originally Posted by da2357
Any final ideas?
Eureka, I found it!

Code:
#!/bin/bash
USERLIST="/Users/johndoe/.bin/xBackup_users"
SRCHFOR="W8735UL8Z5V"
#
USERNAME=`awk -F "," '/^'"$SRCHFOR"'/ {print $2}' $USERLIST`
PASSWORD=`awk -F "," '/^'"$SRCHFOR"'/ {print $3}' $USERLIST`
RSYNCIP=`awk -F "," '/^'"$SRCHFOR"'/ {print $4}' $USERLIST`
#
clear
echo
echo Your account name is $USERNAME
echo Your password is $PASSWORD
echo Your IP address is $RSYNCIP
echo
exit
Thanks to everyone for your ideas.
Sponsored Links
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 09:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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