![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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? |
|
||||
|
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
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
HTH Chris |
|
||||
|
Quote:
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
Any final ideas? |
|
||||
|
Quote:
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
|
| Sponsored Links | ||
|
|