Align the values in column properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Align the values in column properly
# 1  
Old 09-23-2011
Align the values in column properly

This is my input,is it possible to do a bit of scripting on this to produce a properly alligned output.

I/P
Code:
-0.015 0.021 0.056 0.073 0.061 0.020 -0.038
-0.090 -0.113 -0.091 -0.025 0.064 0.141 0.170
0.130 0.027 -0.106 -0.215 -0.249 -0.181 -0.024
0.169 0.319 0.356 0.245 0.011 -0.262 -0.465
-0.497 -0.322 0.017 0.398 0.664 0.682 0.412
-0.073 -0.595 -0.934 -0.920 -0.511 0.174 0.877
1.300 1.224 0.611 -0.347 -1.286 -1.800 -1.609
-0.696 0.646 1.897 2.501 2.100 0.719 -1.188
-2.866 -3.537 -2.733 -0.552 2.279 4.603 5.245
3.519 -0.357 -5.125 -8.708 -8.786 -3.555 7.563
23.529 41.762 58.733 70.895 75.704 72.376 62.138
47.862 33.187 21.390 14.372 12.105 12.694 13.068
10.065 1.538 -12.849 -31.426 -50.960 -67.603 -78.021
-80.350 -74.675 -62.911 -48.148 -33.699 -22.165 -14.821



O/P should like this
Code:
 aaa.bb aaa.bb -cvbgh.kl
-ccc.vb   jjh.jk         0.00

that is decimal point point should be in a single line,whatsoever the values start with

Thanks in advance

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 09-23-2011 at 12:06 PM.. Reason: Please use code tags, thank you
# 2  
Old 09-23-2011
come again... didn't get you...
show us a sample output from your input...

--ahamed
# 3  
Old 09-23-2011
Code:
awk '{printf ("%8.3f %8.3f %8.3f %8.3f .....etc\n", $1, $2, $3, ...)}'

You get a noddy badge if you do it with purely using shell-builtins and without spawning a process for every line of input.
# 4  
Old 09-23-2011
Hi Ahamed,
Sorry,the format got disturbed and its really hard to understand....
well i try to make it bit clear.
the output should not be dodgy as the i/p is, it should be bit aligned....
say after the decimal there are two digits(always) & how about if those two digits always follow a single staright line vertcally top to bottom...

thus all the values(irrespective of signs or number of digits will look properly aligned.),as if they are propely formatted.

Hope that makes sense.
# 5  
Old 09-23-2011
Hi Indra2011,

hartz's answer is correct. I will write the complete and working command. I hope it helps.
Code:
$ cat infile
-0.015 0.021 0.056 0.073 0.061 0.020 -0.038
-0.090 -0.113 -0.091 -0.025 0.064 0.141 0.170
0.130 0.027 -0.106 -0.215 -0.249 -0.181 -0.024
0.169 0.319 0.356 0.245 0.011 -0.262 -0.465
-0.497 -0.322 0.017 0.398 0.664 0.682 0.412
-0.073 -0.595 -0.934 -0.920 -0.511 0.174 0.877
1.300 1.224 0.611 -0.347 -1.286 -1.800 -1.609
-0.696 0.646 1.897 2.501 2.100 0.719 -1.188
-2.866 -3.537 -2.733 -0.552 2.279 4.603 5.245
3.519 -0.357 -5.125 -8.708 -8.786 -3.555 7.563
23.529 41.762 58.733 70.895 75.704 72.376 62.138
47.862 33.187 21.390 14.372 12.105 12.694 13.068
10.065 1.538 -12.849 -31.426 -50.960 -67.603 -78.021
-80.350 -74.675 -62.911 -48.148 -33.699 -22.165 -14.821
$ awk '{ for ( i = 1; i <= NF ; i++ ) { printf "%8.3f", $i }; printf "\n"; }' infile
  -0.015   0.021   0.056   0.073   0.061   0.020  -0.038
  -0.090  -0.113  -0.091  -0.025   0.064   0.141   0.170
   0.130   0.027  -0.106  -0.215  -0.249  -0.181  -0.024
   0.169   0.319   0.356   0.245   0.011  -0.262  -0.465
  -0.497  -0.322   0.017   0.398   0.664   0.682   0.412
  -0.073  -0.595  -0.934  -0.920  -0.511   0.174   0.877
   1.300   1.224   0.611  -0.347  -1.286  -1.800  -1.609
  -0.696   0.646   1.897   2.501   2.100   0.719  -1.188
  -2.866  -3.537  -2.733  -0.552   2.279   4.603   5.245
   3.519  -0.357  -5.125  -8.708  -8.786  -3.555   7.563
  23.529  41.762  58.733  70.895  75.704  72.376  62.138
  47.862  33.187  21.390  14.372  12.105  12.694  13.068
  10.065   1.538 -12.849 -31.426 -50.960 -67.603 -78.021
 -80.350 -74.675 -62.911 -48.148 -33.699 -22.165 -14.821

Regards,
Birei
# 6  
Old 09-23-2011
Yeah, hartz solution will work.

I tried with %5.3f and it didn't work, so I thought it will never work and starting ended making this awk script .
When I was about to paste it, I saw birei's post. hee hee.

I am posting my awk code anyways. It will prefix zero's to make the number in proper format.

Code:
awk '
{ 
        for(i=1;i<=NF;i++){
                a[++k]=$i;
                split($i,_1,".");
                l=length(_1[1]);
                (l>max)?max=l:l=0;
                if(i==NF){
                        a[++k]=xxxx;
                }
        }
}
END {
        for(i=1;i<=k;i++){
                if(a[i] == xxxx){
                        printf "\n";
                        continue;
                }
                split(a[i],_2,".");
                l=length(_2[1]);
                net=max-l;
                if(net > 0){
                        while(net>0){
                                val=val"0";
                                net--;
                        }
                        sub(/^-/,"-"val,a[i]) || sub(/^/,val,a[i])
                        val=""
                }
                printf a[i]" "
        }

}' input_file

--ahamed
# 7  
Old 09-23-2011
thanks all of you,it works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. UNIX for Beginners Questions & Answers

How do I sort data in column properly?

Can i use 'column' command to get the required 3rd column output? Input example: 1 2 345678 90 2 2 356 42 3 3 8265 55 Output required: 1 2 345678 90 2 2 356 42 3 3 8265 55 Basically i want the 3rd column to be justified to the right, instead of left.... (3 Replies)
Discussion started by: nurul_nadzirah
3 Replies

3. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Compare 2 files and match column data and align data from 3 column

Hello experts, Please help me in achieving this in an easier way possible. I have 2 csv files with following data: File1 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:35:47,JOB_5330 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012... (5 Replies)
Discussion started by: asnandhakumar
5 Replies

6. Shell Programming and Scripting

Converting odd values to even values(or vice-versa) located in a column

Hello All, I have a below data in a .csv file where all rows where col1 is A, col2 is odd numbers, similarly even numbers for all rows where col1 is B. Note that my data has some other columns(not shown here) too (around 100) after col2. Tool,Data A,1 A,3 A,5 .... so on B,2 B,4 .... ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. UNIX for Dummies Questions & Answers

shift values in one column as header for values in another column

Hi Gurus, I have a tab separated text file with two columns. I would like to make the first column values as headings for the second column values. Ex. >value1 subjects >value2 priorities >value3 requirements ...etc and I want to have a file >value1 subjects >value2 priorities... (4 Replies)
Discussion started by: Unilearn
4 Replies

8. Shell Programming and Scripting

Cat Values from Several files if it meets criteria for column values

I have results from some statistical analyses. The format of the results are as given below: I want to select lines that have a p-value (last column) less than 0.05 from all the results files (*.results) and cat to a new results file. It would be very nice if a new column is added that tells... (2 Replies)
Discussion started by: genehunter
2 Replies

9. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

10. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question