Searching for fields in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Searching for fields in a file
# 1  
Old 12-11-2006
Question Searching for fields in a file

Hi,
I have a semi colon delimited file of following layout:
FName;Email;LName;Age

eg.

Simon;simon@email.com;Pat;30
Pat;pat@simon@email.com;Simon;25
Sachin;sachin@email.com;Kambli;44


I want to get all records with a particular FName as Simon and LName Pat Smilie How do I dowit?

(Also, my file contains 150K record, so performance is also to be considered!)


Thanks in Advance!
# 2  
Old 12-11-2006
One way:
awk -F\; '$1 == "Simon" && $3 == "Pat"' < file
Make sure you get the quoting right... Pat is followed by a double quote then a single quote.
# 3  
Old 12-12-2006
Thanks for the reply.

Also, there is possibility of two words in FName and LName.
And "Van Ronney" and "VanRonney" should be considered same.
(This check should be put only on FName and LName fields.)

Help Please!
# 4  
Old 12-12-2006
Python alternative:
Code:
for line in open("test.txt"):
 	lline = line.strip().split(";")
 	fname , lname = lline[0].replace(" ","").lower(), lline[2].replace(" ","").lower()
	if fname == 'vanronney' and lname == 'peter':
		print line

# 5  
Old 12-12-2006
Quote:
Originally Posted by Perderabo
One way:
awk -F\; '$1 == "Simon" && $3 == "Pat"' < file
Make sure you get the quoting right... Pat is followed by a double quote then a single quote.
Just to extend this a little:

Code:
name1=Simon
name2=Pat
awk -F\; -v name1=$name1 -v name2=$name2 '$1 == name1 && $3 == name2'  < file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single Field to multiple fields searching

I have a fileA with one column (1000 rows), and fileB with 26 columns(13000 rows). I need to search each value of fileA with fileB and return all the 26 values from FileB to a new file- File C if matches. The search value (from FileA) may present in any of the 26 values in FileB. This value is not... (7 Replies)
Discussion started by: vamsikrishna928
7 Replies

2. Shell Programming and Scripting

Searching the content of one file using the search key of another file

I have two files: file 1: hello.com neo.com,japan.com,example.com news.net xyz.com, telecom.net, highlands.net, software.com example2.com earth.net, abc.gov.uk file 2: neo.com example.com abc.gov.uk file 2 are the search keys to search in file 1 if any of the search key is... (3 Replies)
Discussion started by: csim_mohan
3 Replies

3. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

4. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

5. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

6. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

7. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

8. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

9. Shell Programming and Scripting

searching a log file and appending to a .txt file

I'm new to shell scripting and am writing a script to help me log the free memory and hd space on a server. As of now, the script just runs 'df -h' and appends the output to a file and then runs 'top' and appends the output to a log file. What I want to do, is have the script also search the... (3 Replies)
Discussion started by: enator45
3 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question