ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.