Column printing in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Column printing in awk
# 1  
Old 09-17-2012
Column printing in awk

Experts,

i have a following file containing data in following manner.

Code:
              1      2480434.4   885618.6        0.00        1948.00
                                                                 40.00      1952.00
                                                                 80.00      1961.00
                                                                120.00     1977.00
               2       2480653.8  885738.3        0.00         1948.00
                                                                  40.00       1952.00
                                                                  4000.00   4938.00
                                                                  4040.00   4938.00
               3      2480873.3  885858.0         0.00         1948.00
                                                                  40.00       1952.00
                                                                  80.00       1961.00
                                                                  4100.00   4938.00

Desired output is like below 

    2480434.4    885618.6       0     1948
    2480434.4    885618.6     40     1952
    2480434.4    885618.6     80     1961
    2480434.4    885618.6    120    1977
    2480653.8    885738.3         0    1948
    2480653.8    885738.3        40    1952
    2480653.8    885738.3    4000    4938
    2480653.8    885738.3    4040    4938
    2480873.3    885858             0    1948
    2480873.3    885858           40    1952
    2480873.3    885858           80    1961
    2480873.3    885858         4100    4938

Any help/suggestion on the above request is highly appreciated . I tried a lot for good editing , but i think i am clear with my question above

Thanks & Regards

Last edited by Amit.saini333; 09-17-2012 at 03:49 AM..
# 2  
Old 09-17-2012
Could you please wrap the input and output data in code tags. Not able to understand your input and output format.
[code] ... [/code]
# 3  
Old 09-17-2012
Something which works on your sample:
Code:
awk 'NF==2{$4=$1;$5=$2;$2=p2;$3=p3}
NF==5{p2=$2;p3=$3}
{print $2+0,$3+0,$4+0,$5+0}' OFMT='%.1f' file

Change OFMT value to meet your requirement.

Last edited by elixir_sinari; 09-17-2012 at 04:59 AM..
# 4  
Old 09-17-2012
One way,

Code:
awk '/^$/ {next} NF>4 {f1=$2;f2=$3;print f1,f2,int($4),int($5)} NF<4{print f1,f2,int($1),int($2)}' file

2480434.4 885618.6 0 1948
2480434.4 885618.6 40 1952
2480434.4 885618.6 80 1961
2480434.4 885618.6 120 1977
2480653.8 885738.3 0 1948
2480653.8 885738.3 40 1952
2480653.8 885738.3 4000 4938
2480653.8 885738.3 4040 4938
2480873.3 885858.0 0 1948
2480873.3 885858.0 40 1952
2480873.3 885858.0 80 1961
2480873.3 885858.0 4100 4938


If you don't have blank lines, Remove the first part of the command.
# 5  
Old 09-17-2012
Dear clx,

your suggestion worked well but i want the third and fourth column to right padded like following

i tried following in your given code though it is working fine but when the next first values changes it gets distorted

Code:
awk '/^$/ {next} NF>4 {f1=$2;f2=$3;print f1,f2,int($4),int($5)}  NF<4{print (%-10s %-10s %-5s %-5s f1,f2,int($1),int($2))}' file

Code:
2480434.4 885618.6       0  1948 
2480434.4 885618.6     40  1952 
2480434.4 885618.6     80  1961 
2480434.4 885618.6   120  1977 
2480653.8 885738.3       0 1948 
2480653.8 885738.3     40 1952 
2480653.8 885738.3 4000 4938 
2480653.8 885738.3 4040 4938 
2480873.3 885858.0       0 1948 
2480873.3 885858.0     40 1952 
2480873.3 885858.0     80  961 
2480873.3 885858.0  4100 4938


THANKS
AMIT

Last edited by Franklin52; 09-17-2012 at 08:17 AM.. Reason: adding code tags
# 6  
Old 09-17-2012
Code:
xargs -n 11 < file | sed 's:\(^.\|\.00\)\( \|$\): :g' | awk '{gsub(/^ /,"");for(i=5;i<NF;i+=2)$i=RS$1FS$2FS$i}1'

# 7  
Old 09-17-2012
Thanks a lot .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: printing column using for loop

Hello: I've input data: Input data --- 3:60069:C:T 60069 C T 1 0 0 1 0 0 1 0 0 1 0 0 1 --- 3:60079:A:G 60079 A G 1 0 0 0.988 0.012 0 1 0 0 1 0 0 1 --- rs186476240:60157:G:A 60157 G A 1 0 0 1 0 0 1 0 0 1 0 0 1 I edit/make first few columns before numbers (6th column) and want to... (4 Replies)
Discussion started by: genome
4 Replies

2. Shell Programming and Scripting

awk: printing newline with last column

I was trying to simplify this from what I'm actually doing, but I started getting even more confused so I gave up. Here is the content of my input file: Academic year,Term,Course name,Period,Last name,Nickname 2012-2013,First Semester,English 12,7th Period,Davis,Lucille When I do this: ... (3 Replies)
Discussion started by: nextyoyoma
3 Replies

3. Shell Programming and Scripting

Printing another column using awk and input data

Hi, I have data of the following type, chr1 234 678 39 852 638 abcd 7895 chr1 526 326 33 887 965 kilj 5849 Now, I would like to have something like this chr1 234 678 39 852 638 abcd 7895 <a href="http://unix.com/thread=chr1:234-678">Link</a> chr1 526 326 33 887 965 kilj 5849 <a... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

5. Solaris

Printing a particular column[autosys]

Dear All, I'm using autosys in my production system. My concern is as follows: autosys -j <some_job_nm> Output: Job Name Last Start Last End ST Run Pri/Xit ... (1 Reply)
Discussion started by: saps19
1 Replies

6. UNIX for Dummies Questions & Answers

creating a file using the fist column and printing second column

Hello all. I have a problem that I need help solving. I would like to convert the following file: human pool1_12 10e-02 45 67 human pool1_1899 10e-01 45 29 human pool1_1829 10e-01 43 26 horse pool1_343 10e-20 65 191 horse pool1_454 10e-09 44 43... (5 Replies)
Discussion started by: viralnerd
5 Replies

7. Shell Programming and Scripting

regarding about printing row to column

Hello, I got data like that, =111 A= alpha B= 1 C= qq D= 45 F= ss G= 334 =1234 A= B= 2w C= D= 443 F= G= =3434 A= B= e3e (5 Replies)
Discussion started by: davidkhan
5 Replies

8. UNIX for Dummies Questions & Answers

Printing highest value from one column

Hi, I have a file that looks like this: s6 98 s6 91 s6 56 s5 32 s5 10 s5 4 So what I want to do is print only the highest value for each value in the column: So the file will look like this: s6 98 s5 32 Thanks (4 Replies)
Discussion started by: phil_heath
4 Replies

9. Shell Programming and Scripting

Printing 1st column to lower case using awk

I want to print the 1st field in a comma seperated file to lower case and the rest the case they are. I tried this nawk -F"," '{print tolower($0)}' OFS="," file this converts whole line in to lower case i just want the first column to be converted. The below doesnt work because in... (11 Replies)
Discussion started by: pinnacle
11 Replies

10. Shell Programming and Scripting

Awk not printing the last combined column

nawk -F "|" 'FNR==NR {a=$2 OFS $3 OFS $4 OFS $5 OFS $6;next}\ {if ($5 in a)print $1,"test",$5,a, $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 ; \ else print $1,"Database",$5 OFS OFS OFS OFS OFS OFS $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2-$3-$4 }' OFS="|" \ file1 file2 > file3 This... (5 Replies)
Discussion started by: pinnacle
5 Replies
Login or Register to Ask a Question