Printing Formatted Records From A File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing Formatted Records From A File
# 1  
Old 09-03-2008
Question Printing Formatted Records From A File

Hi All,

I'm writing a script that provides a menu to manipulate records. I'm having a problem, however. The first option I'm writing is simply to display all of the records in a supplied file.

The supplied file ('records') currently contains sample data, this data is:

95671660:Jones:Sarah:45:sales manager
93272658:Smith:John:43:technical manager
98781987:Williams:Nick:35:computer officer
99893878:Brown:Sarah:12:electrician
95673456:Couch:David:26:chef
95437869:Anderson:Sarah:19:CEO

I need to display this data as standard output. It needs to be arranged in 5 columns, each left-justified. The column order is family name (i.e. Jones), first name, telephone number , department number (i.e. 45) and job title. In needs to be sorted in ascending alphabetical order of family name (i.e. Anderson, Brown etc.)

Basically, it should look (something) like this:

Code:
Anderson    Sarah   95437869   19   CEO
Brown       Sarah   99893878   12   electrician
Couch       David   95673456   26   chef
Jones       Sarah   95671660   45   sales manager
Smith       John    93272658   43   technical manager
Williams    Nick    98781987   35   computer officer

I've been trying various solutions using cut and gawk. It's an sh script. Honestly, though, it's getting stupid. Look:

Code:
                # Print records option.
                1)      echo -e "Debug: User has selected print option.\n"
                        #cut -f2 -d: records | sort
                        awk '$2 $3 $1 $4 $5 {print}' records
                        #cut -f3 -d: records
                        #cut -f1 -d: records
                        #cut -f4 -d: records
                        #cut -f5 -d: records
                        echo -en "\nPress Enter to continue... "
                        read
                        ;;

It's pretty obvious I haven't a clue how to use (g)awk properly, lol. I'm used to using cut for extracting single columns. I've never had to extract and format a table like this, using unix.

Can anyone help?

Thanks in advance,
Wolfie

Last edited by Silentwolf; 09-03-2008 at 03:09 AM.. Reason: Neatening up formatting.
# 2  
Old 09-03-2008
Have a look at the printf function in awk.
# 3  
Old 09-03-2008
You can use printf to format the output, it should be something like:

Code:
awk -F":" '{printf("%-12s%-8s%-11s%-5s%s\n",$2, $3, $1, $4, $5)}' records | sort

Regards
# 4  
Old 09-03-2008
Thanks very much Franklin, that's exactly what I was after!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing a formatted file with awk

Hi - I want to interrogate information about my poker hands, sessions are all recorded in a text file in a particular format. Each hand starts with the string <PokerStars> followed by a unique hand reference and other data like date/time. There is then all the information about each hand. My first... (5 Replies)
Discussion started by: rbeech23
5 Replies

2. UNIX for Beginners Questions & Answers

Help in printing records where there is a 'header' in the first record ???

Hi, I have a backup report that unfortunately has some kind of hanging indent thing where the first line contains one column more than the others I managed to get the output that I wanted using awk, but just wanting to know if there is short way of doing it using the same awk Below is what... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Separate records of a file on 2 types of records

Hi I am new to shell programming in unix Please if I can provide help. I have a file structure of a header record and "N" detail records. The header record will be the total number of detail records I need to split the file in 2: One for the header Another for all detail records Could... (1 Reply)
Discussion started by: jamcogar
1 Replies

4. UNIX for Dummies Questions & Answers

Printing records in different format

Hi all, I have a input file say record.txt hostname IP_address Port_No Version A 10.10.10.1 80 6.02 B 10.10.10.2 81 6.03 C 10.10.10.3 82 6.04 row 1 has 4 field headings : hostname, IP_address, Port_No and Version. and from 2nd row onwards the actual records start. now i need to... (2 Replies)
Discussion started by: PranavEcstasy
2 Replies

5. Shell Programming and Scripting

Deleting duplicate records from file 1 if records from file 2 match

I have 2 files "File 1" is delimited by ";" and "File 2" is delimited by "|". File 1 below (3 record shown): Doc1;03/01/2012;New York;6 Main Street;Mr. Smith 1;Mr. Jones Doc2;03/01/2012;Syracuse;876 Broadway;John Davis;Barbara Lull Doc3;03/01/2012;Buffalo;779 Old Windy Road;Charles... (2 Replies)
Discussion started by: vestport
2 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Problem with formatted printing in AWK

Hi, I want to print 130 fileds using formatted printing in AWK. It looks like awk '{printf ("%7.2f%7.2f...%7.2\n",$1,$2,...,$130)}' inflie>oufile But it gives me an error: Word too long! Can you please help me with this? Is there another way to do this? (1 Reply)
Discussion started by: PHL
1 Replies

8. Shell Programming and Scripting

Printing records which meet condition using awk

Below is the code nawk -F"|" 'tolower($1) ~ "abc" |"" {if (tolower($2) ~"abc"|"") print$0}' file I want the records from file whose 1st and 2nd field should be either "abc" or "null" I tried but its giving error. Appreciate help (2 Replies)
Discussion started by: pinnacle
2 Replies

9. Shell Programming and Scripting

Generating a report -Formatted printing -Urgent

Hi, My aim is to generate a report using shell script. There are various formats fields coloumns etc. I want to print in a single line (row) but in different coloumn as given below: field1 field2 field3 field4 ....... ....... ...... ....... The spacing... (1 Reply)
Discussion started by: jisha
1 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question