LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2016, 01:21 PM   #1
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Rep: Reputation: Disabled
awk format output


New to awk. Just trying some simple examples and not quiet getting it. My data is separated with ":" and I want the output to be spaced in ordered columns. I used this command and it seems to work on the first three columns.

Code:
awk -F: 'BEGIN {format = "%-10.10s  %s\t %s\n"} {printf format, $1, $2, $3 $4 $5}' data
Quote:
101:Johnson:John:K:39000
102:King:Mary:K:39800
103:Brown:Gretchen:K:35000
104:Adams:Betty:C:42000
105:Utley:Amos:V:36000
106:Wilson:Patricia:B:39000
107:Culligan:Thomas:F:39000
108:Mitchell:Hillary:N:32800
109:Arbuckle:Margaret:F:46700
110:Ford:Terrence:H:44700
111:Greene:Sarah:L:41700
112:Rose:Richard:P:40200
113aniels:Allan:S:30500
114:Edwards:George:J:38500
Code:
Output data:
101         Johnson      JohnK39000
102         King         MaryK39800
103         Brown        GretchenK35000
104         Adams        BettyC42000
105         Utley        AmosV36000
106         Wilson       PatriciaB39000
107         Culligan     ThomasF39000
108         Mitchell     HillaryN32800
109         Arbuckle     MargaretF46700
110         Ford         TerrenceH44700
111         Greene       SarahL41700
112         Rose         RichardP40200
113         Daniels      AllanS30500
114         Edwards      GeorgeJ38500

Last edited by lamont0; 02-14-2016 at 01:23 PM. Reason: code block did not work for one item.
 
Old 02-14-2016, 01:40 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
An expression like "$3 $4 $5" causes those strings to be concatenated with no intervening space. If you want to pass those fields as one argument but with intervening spaces, you'll have to include the spaces in the expression:
Code:
awk -F: 'BEGIN {format = "%-10.10s  %s\t %s\n"} {printf format, $1, $2, $3 " " $4 " " $5}' data
 
Old 02-14-2016, 01:42 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by lamont0 View Post
... I want the output to be spaced in ordered columns.
There is a Linux command made for this purpose.

With this InFile ...
Code:
101:Johnson:John:K:39000
102:King:Mary:K:39800
103:Brown:Gretchen:K:35000
104:Adams:Betty:C:42000
105:Utley:Amos:V:36000
106:Wilson:Patricia:B:39000
107:Culligan:Thomas:F:39000
108:Mitchell:Hillary:N:32800
109:Arbuckle:Margaret:F:46700
110:Ford:Terrence:H:44700
111:Greene:Sarah:L:41700
112:Rose:Richard:P:40200
113:Daniels:Allan:S:30500
114:Edwards:George:J:38500
... this code ...
Code:
column -ts':' $InFile >$OutFile
... produced this OutFile ...
Code:
101  Johnson   John      K  39000
102  King      Mary      K  39800
103  Brown     Gretchen  K  35000
104  Adams     Betty     C  42000
105  Utley     Amos      V  36000
106  Wilson    Patricia  B  39000
107  Culligan  Thomas    F  39000
108  Mitchell  Hillary   N  32800
109  Arbuckle  Margaret  F  46700
110  Ford      Terrence  H  44700
111  Greene    Sarah     L  41700
112  Rose      Richard   P  40200
113  Daniels   Allan     S  30500
114  Edwards   George    J  38500
Daniel B. Martin
 
Old 02-14-2016, 04:11 PM   #4
lamont0
LQ Newbie
 
Registered: Feb 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
awk format output

Great, thank you! I did not even notice that I left out the commas. Thank you for the column command. Another great option. This is what I found that works.
Code:
awk -F: '{printf("%-10s%-10s%-10s%-10s%-10s\n", $1, $2, $3, $4, $5)}'
Code:
Results:
101       Johnson   John      K         39000
102       King      Mary      K         39800
103       Brown     Gretchen  K         35000
104       Adams     Betty     C         42000
105       Utley     Amos      V         36000
106       Wilson    Patricia  B         39000
107       Culligan  Thomas    F         39000
108       Mitchell  Hillary   N         32800
109       Arbuckle  Margaret  F         46700
110       Ford      Terrence  H         44700
111       Greene    Sarah     L         41700
112       Rose      Richard   P         40200
113       Daniels   Allan     S         30500
114       Edwards   George    J         38500
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
print output in desire format using awk or cut command dipanjan Linux - General 1 12-15-2012 01:59 AM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 09:17 AM
[SOLVED] Bash; awk or sed output to variable: how keep newline at end of each output line porphyry5 Programming 3 06-10-2011 05:50 PM
awk: How can I keep the format of the record? quanba Programming 5 04-05-2010 09:50 PM
Best video format for YouTube upload and can Recordmydesktop output that format? linus72 Linux - Software 6 12-21-2009 03:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration