Help creating well-formatted columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help creating well-formatted columns
# 1  
Old 04-14-2011
Help creating well-formatted columns

Hi all

I'm having a few issues with sorting some data into easily-readable columns.

Original data in file:
Code:
Number of visits  IP Address
      8 244.44.145.122
      8 234.45.165.125
      6 225.107.26.10

I firstly tried the column -t command which results in this:

Code:
Number  of               visits  IP  Address
8       244.44.145.122
8       234.45.165.125
6       225.107.26.10

I also understand the pr command can be used, but I'm not sure how it could be used to split this into nicely formatted columns so the headings line up with the columns?

Any tips would be appreicated. Smilie
# 2  
Old 04-14-2011
That's a job for printf which stands for "print format(ted)". Example with awk to pass the header in the 1st line/record, since there is no distinguished delimeter between the 2 headers "Number of visits" and "IP Address":

Code:
awk 'NR>1 {printf("%-18s%-20s\n", $1,$2); next}1' infile
Number of visits  IP Address
8                 244.44.145.122
8                 234.45.165.125
6                 225.107.26.10

printf is available in shells as well as in most other programming/scripting languages.
# 3  
Old 04-14-2011
Wow, thanks a lot!

If you wish to explain how some of this works, can you do so?

I understand the 18s is the spacing for the first column, so what is the 20s doing? If I reduce the number it makes no difference at all. Unless that's setup incase a 3rd column is going to be used.

And what does the NR>1 1 mean? I should really familiarise myself with Unix syntax...

Thanks Smilie
# 4  
Old 04-14-2011
NR>1 If the line number ie. "Number Rows" is greater than 1, ie. line 2,3,4,5,6, ...

%-18s- -> aligned left, 18 -> characters wide, s -> type string

next just cancel the processing of the awk commands and take the next line/row

}1If the NR>1 is not true, just print out the line, which is in this case the header, since it has a value of NR==1, ie. is the 1st row in the input file.
# 5  
Old 04-14-2011
Okay, thanks very much for the help. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a loop for multiplying columns

I have 2 files, that look like this: ID SNP1 SNP2 SNP3 SNP4 A1 1 2 0 2 A2 2 0 1 1 A3 0 2 NA 1 A4 1 1 0 2 and this: SNP score SNP1 0.5 SNP2 0.7 SNP3 0.8 SNP4 0.2 Basically, all of the SNP-values are 0,1, 2 or NA, and they each have a score, listed in the second file. The total... (5 Replies)
Discussion started by: kayakj
5 Replies

2. Shell Programming and Scripting

Creating subset of a file based on specific columns

Hello Unix experts, I need a help to create a subset file. I know with cut comand, its very easy to select many different columns, or threshold. But here I have a bit problem as in my data file is big. And I don't want to identify the column numbers or names manually. I am trying to find any... (7 Replies)
Discussion started by: smitra
7 Replies

3. Homework & Coursework Questions

Creating a .profile, displaying system variables, and creating an alias

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Here is what I am supposed to do, word for word from my assignment page: 1. Create/modify and print a... (2 Replies)
Discussion started by: Jagst3r21
2 Replies

4. UNIX for Dummies Questions & Answers

Creating columns from a list

I have a list below, how can I have things separated nicely in columns mv browseDir.tcsh browseDir.csh mv checkSRDist.tcsh checkSRDist.csh mv create-data-tinv.tcsh create-data-tinv.csh mv createDocs.tcsh createDocs.csh mv createMisfit.tcsh createMisfit.csh mv createModel.tcsh... (4 Replies)
Discussion started by: kristinu
4 Replies

5. Solaris

Creating script adding 3 different variables in 3 columns

I have 3 variables with different information.. they look like this (row-wise aswell): Variable1 = Roland Kalle Dalius Variable2 = ake123 ler321 kaf434 Variable3 = Richardsen Sworden Lokthar How can I sort them by variable3 alphabetical and add them into the same output so... (0 Replies)
Discussion started by: Prantare
0 Replies

6. Programming

Creating a table like format with rows and columns

I have few files which have two columns in each. like e2 1 1 2694 2 4 2485 3 2 2098 5 1 2079 6 5 2022 9 4 1734 11 5 1585 13 2 1461 18 1 1092 21 2 1019 24 1 915 25 3 907 27 1 891 28 3 890 34 1 748 39 1 700 (1 Reply)
Discussion started by: kamuju
1 Replies

7. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies

8. Shell Programming and Scripting

creating & sending formatted (with bolds & colors) CSV

Hi , I have a situation. Need is to create & send a formatted file with header in BOLD & colored & some sequel results as a content. I know echo -e \033 command, but its scope is limited in PUTTY. How to retain the formatting out of Putty; say after someone opens a email attachment... (2 Replies)
Discussion started by: infaWorld
2 Replies

9. Shell Programming and Scripting

cut - columns with formatted Output

Hi I have the input file as below ***TEST10067 00567GROSZ 099 00567CTCTSDS90 ***TEST20081 08233GROZWEWE 00782GWERW899 ***TEST30088 08233GROZWEWE 00782GWERW899 I am finding the lines starting with *** and outputing as below TEST10067 TEST20081 TEST30088 I need a space between TEST1... (9 Replies)
Discussion started by: dhanamurthy
9 Replies

10. Shell Programming and Scripting

Formatted Output

Hi I have the following lines in a file SWPRC000001NOT STATED 1344 SWPRC000001NOT STATED 1362 SWPRC000001NOT STATED 1418 SWPRC000001NOT STATED 1436 SWPRC000001NOT STATED ... (6 Replies)
Discussion started by: dhanamurthy
6 Replies
Login or Register to Ask a Question