Search flat file and return 3 fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search flat file and return 3 fields
# 1  
Old 11-17-2008
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  
Old 11-17-2008
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  
Old 11-18-2008
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  
Old 11-18-2008
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  
Old 11-18-2008
Quote:
Originally Posted by in2nix4life
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  
Old 11-18-2008
Hammer & Screwdriver

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  
Old 11-18-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search for blank fields in a text file from a certain position?

Sample txt file : OK00001111112| OK00003443434|skjdaskldj OK32812983918|asidisoado OK00000000001| ZM02910291029|sldkjaslkjdasldjk what would be the shell script to figure out the blank space (if any) after the pipe sign? (4 Replies)
Discussion started by: chatwithsaurav
4 Replies

2. Shell Programming and Scripting

awk to delete fields from flat file

I would like to know awk command to delete the specific feild from a flat file delimited by '|' if the first feild starts with 354 I tried awk 'BEGIN {FS=OFS="|"} {$16=""; sub(/\|/, "")}'1 Input file: 354|||||GROUND||97||JUDD STREET|||LONDON||WC1H 9JG|ADI001| 354|||||FLAT 1... (10 Replies)
Discussion started by: Aditya_001
10 Replies

3. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

6. UNIX for Dummies Questions & Answers

Search flat file in specific byte

I am on AIX Unix. I want to read a flat file for a string in a certain byte. I want to find the value: 943034 in column 56; and write out just those records to another file. Also, could I get the line/record number of where it was found in the input file? Thank you, sboxtops (1 Reply)
Discussion started by: sboxtops
1 Replies

7. Shell Programming and Scripting

`find`, pulling 1st field from ASCII flat file as search/-name?

Hey Everyone! I have searched around for this on Unix.com and Google, and I'm either not phrasing my search properly or this is not as simple as I thought... I have a script that runs on a nightly basis that pulls one field worth of data from an internal MySQL database and populates to an... (2 Replies)
Discussion started by: Gecko12332
2 Replies

8. Shell Programming and Scripting

search fields within a file

I need to be able to search multiple fields within a file that contain blank (nothing). Not sure what command to use? thought it would be grep but not sure how to write it The reason is i have a script that when it does this and identifies a field which has a blank it errors out of the script... (4 Replies)
Discussion started by: Pablo_beezo
4 Replies

9. UNIX for Dummies Questions & Answers

Search for String within File and Return File Name

How do I search for a string within a file and return the names of the file that contain the string? I would like to search directories and sub-directories. (4 Replies)
Discussion started by: bggibson
4 Replies

10. UNIX for Dummies Questions & Answers

Shell script to return all the ID's from file based on the distribution ID search

Hi, Please help me out on this I have to store some distribution lists(dl) in a file say mail.txt Say the file format be d1 = alok@yahoo.co.in;alvin@rediffmail.com;vijay@yahoo.com; & so on... d2 = abhinav@gmail.com;dalpit@hotmail.com;garima@yahoo.com;& so on... d3 = .... & so on Now what... (1 Reply)
Discussion started by: kumbhatalok
1 Replies
Login or Register to Ask a Question