using awk to format text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to format text
# 1  
Old 05-21-2009
using awk to format text

I'm new to awk and would appreciate a jump start.

I've got a text doc of people with first and last names, ages, home cities, and a phrase about the individual. I want to parse the text into fields and rows separated by tabs with the field names of - Firstname, Lastname, Age, City, Dollar Amount, and Synopsis.

Here's how the text reads:

- Larry Denman, 54, of Compton, who submitted false claims totaling $3,394 for services to his mother who, in fact, was hospitalized and later died. - Louwanda Hurt, 40, of Los Angeles, who submitted false claims totaling $1,488 for services to her client who, in fact, had died. - Shirley Byrd, 48, of Long Beach, who submitted false claims totaling $2,953 for services to her mother who, in fact, had died. - Rufus Jackson, 34, of Los Angeles, who submitted false claims totaling $6,506 for services to her client who, in fact, had died.

While it may not be elegant, the order appears pretty consistent.

Any help would be much appreciated.
# 2  
Old 05-21-2009
so how should the output look like, and what have you tried?
# 3  
Old 05-21-2009
format

I want the output to look like this
Firstname\tLastname\tAge\tCitytDollar Amount\tSynopsis\n
xxxx\txxx \txx\txx\t$n,nnn\txxxxxxxxxxxxxx

And, I apologize, but I'm looking for a starting point. But I do appreciate your response.
# 4  
Old 05-21-2009
if you have Python
Code:
#!/usr/bin/env python
for line in open("file"):
    line=line.strip().split("-")
    for item in line:
        if item!="":
            print '\t'.join(item.split(",",3))

output
Code:
 ./test.py
 Larry Denman    54      of Compton      who submitted false claims totaling $3,394 for services to his mother who, in fact, was hospitalized and later died.
 Louwanda Hurt   40      of Los Angeles  who submitted false claims totaling $1,488 for services to her client who, in fact, had died.
 Shirley Byrd    48      of Long Beach   who submitted false claims totaling $2,953 for services to her mother who, in fact, had died.
 Rufus Jackson   34      of Los Angeles  who submitted false claims totaling $6,506 for services to her client who, in fact, h

# 5  
Old 05-21-2009
hope this will help
Code:
awk 'BEGIN{RS="-"}{print $0}' filename|sed 's/,/  /g'

# 6  
Old 05-22-2009
Thanks for both suggestions. These will get me on the path once I figure out what they're doing. I'll deconstruct with a new sed awk book I've ordered.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format the text using sed or awk

I was able to figure out how to format a text. Raw Data: $ cat test Thu Aug 23 15:43:28 UTC 2018, hostname01, 232.02, 3, 0.00 Thu Aug 23 15:43:35 UTC 2018, hostname02, 231.09, 4, 0.31 Thu Aug 23 15:43:37 UTC 2018, hostname03, 241.67, 4, 0.43 (5 Replies)
Discussion started by: kenshinhimura
5 Replies

2. Shell Programming and Scripting

Converting text files to xls through awk script for specific data format

Dear Friends, I am in urgent need for awk/sed/sh script for converting a specific data format (.txt) to .xls. The input is as follows: >gi|1234|ref| Query = 1 - 65, Target = 1677 - 1733 Score = 8.38, E = 0.6529, P = 0.0001513, GC = 46 fd sdfsdfsdfsdf fsdfdsfdfdfdfdfdf... (6 Replies)
Discussion started by: Amit1
6 Replies

3. Shell Programming and Scripting

Help with awk statement to format text file.

Hello, I am fairly new to shellscripting and have written a script to check on messages file and report failed logins: Here is the original file: Jul 17 03:38:07 sfldmilx086 sshd: error: PAM: Authentication failure for houghn97 from 10.135.77.201 Jul 17 03:38:07 sfldmilx086 sshd: error:... (2 Replies)
Discussion started by: neilh1704
2 Replies

4. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

5. Shell Programming and Scripting

Text Format

I'd like to format a text in unix before mailing it to users. I want to change color and font of my plain text in unix. Thanks.. (1 Reply)
Discussion started by: Sara_84
1 Replies

6. Shell Programming and Scripting

awk or sed to format text file

hi all, i have a text file which looks like the below 01 02 abc Top 40 music Kidz Only! MC 851 MC 852 MC 853 7NOW Arch_Diac xyz2 abc h211 Commacc1 Commacc2 Commacc3 (4 Replies)
Discussion started by: posner
4 Replies

7. Programming

awk script to convert a text file into csv format

hi...... thanks for allowing me to start a discussion i am collecting usb usage details of all users and convert it into csv files so that i can export it into some database.. the input text file is as follows:- USB History Dump by nabiy (c)2008 (1) --- Kingston DataTraveler 130 USB... (2 Replies)
Discussion started by: certteam
2 Replies

8. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

9. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

10. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies
Login or Register to Ask a Question