Need a very specific awk command for parsing records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a very specific awk command for parsing records
# 1  
Old 05-01-2012
Need a very specific awk command for parsing records

I have data in a file where the first line of the file identifies the fields. I need to be able to parse out any single record, field-by-field, with the fields identified. So if my file looks like this:
Code:
NAME, ADDRESS, PHONE
Jim Smith, 123 Main Street, 999-888-7777
Bob Jones, 555 Park Avenue, 666-555-4444
Mary Johnson, 753 Oak Street, 333-222-1111

I need to be able to query by name, say for "Bob Jones" and get the following output:
Code:
NAME: Bob Jones
ADDRESS: 555 Park Avenue
PHONE: 666-555-4444


Last edited by Franklin52; 05-01-2012 at 11:25 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 05-01-2012
Code:
name='Bob Jones'
awk -F, -v "name=$name" '$1==name{printf("NAME: %s\nADDRESS: %s\nPHONE: %s\n",$1,$2,$3)}' file

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 05-01-2012
Or possibly, if it's easier, could I just print out all records in the following format:

Code:
NAME: Jim Smith
ADDRESS: 123 Main Street
PHONE: 999-888-7777
 
NAME: Bob Jones
ADDRESS: 555 Park Avenue
PHONE: 666-555-4444
 
NAME: Mary Johnson
ADDRESS: 753 Oak Street
PHONE: 333-222-1111


Last edited by Franklin52; 05-01-2012 at 11:25 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 05-01-2012
Quote:
Originally Posted by Finja
Or possibly, if it's easier, could I just print out all records in the following format:
Sure, just remove the conditional statement $1==name
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 05-01-2012
Any way I could read in the field identifiers from the first line of the file instead of having to specify them? Would like for this statement to work on multiple different files where the records aren't exactly the same.

Thanks for the quick responses.
# 6  
Old 05-01-2012
OK. Sure. To make simple report from csv perhaps.

Code:
awk -F, 'NR==1{for (i=1;i<=NF;i++) h[i]=$i; next} {for (i=1;i<=NF;i++) printf("%s: %s\n", h[i], $i); print ""}'

If you plan to actually have spaces after the comma, maybe -F',[[:space:]]*' will be better delimiter so that it is trimmed off.
This User Gave Thanks to neutronscott For This Post:
# 7  
Old 05-01-2012
Perfect. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk command to copy field to bottom records

Hi All, I have a below input, I want to copy the Job_name and Created_by field information to other bottom records as shown in below Output Job_name Created_by Modified_on Modified_by CGI_ACLMIB n38504 2014-05-07 20:40:48 n38504 2014-05-07 20:40:57 n38504 2014-05-08 20:40:57 n48504... (1 Reply)
Discussion started by: somu_june
1 Replies

2. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Parsing records in file

have a datafile that contains: Jun 13 12:59:21 Jun 13 13:02:04 Jun 13 13:14:21 i want to parse this so they look like this: Jun 13 12:59 Jun 13 13:02 Jun 13 13:14 how can i do this? basically wanna get rid of the seconds. i'd prefer this to be a one liner. command | awk... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

5. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

6. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

7. UNIX for Dummies Questions & Answers

AWK Command to find text in specific column

I'm new to scripting and would appreciate any help. I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches. I originally used a grep command which works, but provides File1 results that can be found... (3 Replies)
Discussion started by: Chillin
3 Replies

8. Shell Programming and Scripting

Parsing record into multiple records in Shell Script

Hi, I am trying to parse a very long record in a text file into multiple records by checking ADD, DELETE, or MODIFY field value in a shell script. Input # File name xyz.txt ADD|N000|8015662|DELETE|N001|9915662|MODIFY|N999|85678 Output ADD|N000|8015662| DELETE|N001|9915662|... (8 Replies)
Discussion started by: naveed
8 Replies

9. UNIX for Advanced & Expert Users

Parsing records from one record

Hi, I got a file which is one huge record. I know each record should be 550 bytes long. How do I parse out the records from the one huge record. (1 Reply)
Discussion started by: bwrynz1
1 Replies

10. UNIX for Dummies Questions & Answers

Parsing out records from one huge record

Hi, I have one huge record and know that each record in the file is 550 bytes long. How do I parse out individual records from the single huge record. Thanks, (4 Replies)
Discussion started by: bwrynz1
4 Replies
Login or Register to Ask a Question