How to print the output in correct order?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print the output in correct order?
# 1  
Old 06-20-2013
How to print the output in correct order?

Hi,

while using following awk commend I’m getting confused,
The output is not like as the row present in input files, can anyone explain and tell me how to print in the order like in input.

Code:
value=$(awk 'FNR>1 && NR==FNR{a[$2]=$4;next} a[$2]{sum[$2]+=$4} END {for(i in sum){printf i"\t"sum[i]/2"@@";}}' file1.tsv file2.tsv)
echo $value > file.tsv
sed -i 's|@@|\n|g' file.tsv


File1.tsv
Code:
Col1 col2 col3 col4 
1 952 sa 140
2 124 sb 130
3 950 sc 92
4 125 sd 150
5 800 se 110

File2.tsv
Code:
Col1 col2 col3 col4 
1 952 sa 130
2 124 sb 150
3 950 sc 90
4 125 sd 160
5 800 se 100

Result file.tsv
Code:
col1 col2
950 46
952 70
124 65
800 55
125 75


Last edited by Don Cragun; 06-20-2013 at 05:35 AM.. Reason: Fixed CODE tag for code segment.
# 2  
Old 06-20-2013
As the order in which array elements are retrieved in a (for i in arr) command is undefined (c.f. awk man page), there's not much you can do. One option might be to use sequence numbers as indexes into the array, and then print according to that sequence.

BTW, the result that you present is not the result of your command line applied to the sample files as posted.
# 3  
Old 06-20-2013
And also your awk seems to be doing things only with file1.tsv. What would you really require to do with both files ? If you want to preserve order, it is better to print them as it gets processed than using array indexes.
# 4  
Old 06-20-2013
Hi raja,

what I'm doing is

1st I'm matching the file1.tsv 2nd col value to file2.tsv then take
the average of the 4th col with the corresponding value of col2,
that so I'm printing 2sd col value and result value , but the printed flow is changing

---------- Post updated at 03:22 PM ---------- Previous update was at 03:10 PM ----------

Yes rudic, the result of above is,
this can be get only after removed the next word

Code:
col1 col2
950 91
952 135
124 140
800 105
125 155

# 5  
Old 06-20-2013
Code:
awk 'FNR>1 && NR==FNR{a[$2]=$4;next;} FNR>1 {a[$2]+=$4; print $2"\t"a[$2]/2}' f1 f2

# 6  
Old 06-20-2013
Thanks Raja its working fine
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Now showing the correct output

Hello I am working on one script where I am trying to display all the directories which is inside the workspace but somehow it is giving me weird output and this is occurring only with one directory other also having the result.html file inside the directory. for i in `ls -1 | egrep -iv... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

2. Shell Programming and Scripting

print in reverse order

Hi, I want to print the item in reverse order such that the output would look like 00 50 50 23 40 22 02 96 Below is the input: 00 05 05 32 04 22 20 69 Video tutorial on how to use code tags in The UNIX and Linux Forums. (5 Replies)
Discussion started by: reignangel2003
5 Replies

3. Shell Programming and Scripting

print in incremental order a sentence

Dear help! I want to print The number i is number i let i=1 to 5 output should be like The number 1 is number 1 The number 2 is number 2 The number 3 is number 3 The number 4 is number 4 The number 5 is number 5 Would be gr8 if you mke this with awk Thanks (7 Replies)
Discussion started by: Indra2011
7 Replies

4. Shell Programming and Scripting

Unable to get the correct sort order in perl.

Hi, I have created the hash. %hash; @arr1 = qw(Dealnum AdminStatus adminReason effFrom effTo); @arr2 = qw(121212121 YES 1992-06-19T05:14:27 ); @hash{@arr1}=@arr2; foreach(sort keys %hash){ print "$_ ---- $hash{$_}\n"; } The output i got like this: C:\strawberry\perl\bin>perl... (1 Reply)
Discussion started by: vanitham
1 Replies

5. UNIX for Dummies Questions & Answers

How to print arguments in reverse order?

Hey all, How do I make a script print its arguments in reverse order? Thanks (5 Replies)
Discussion started by: unclepickle1
5 Replies

6. Shell Programming and Scripting

diff output is it correct??

I'm asking for explanation about the output of the diff format when i compare the two files f1 and f2: root@host1 # cat f1 205226 205237 205250 205255 205262 205274 205307 205403 205464 205477 205500 205520 205626 205759 205766 205776 (2 Replies)
Discussion started by: ahmad.zuhd
2 Replies

7. Shell Programming and Scripting

Getting the correct identifier in the output file

Hi All I do have a file like this: 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 8.9e+016 GCGGTAACT 4 20 26 232 1.7e+013 TTTTTATTTTTTTTTTTTCC 5 ... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

8. Shell Programming and Scripting

Regarding about the print line number/order

Hello, I am trying to print line number/order using this command awk '{print $0, FNR}' myfilename 11006 A41 1888 11006 A41 1888 11006 A41 1888 11006 A41 ... (2 Replies)
Discussion started by: davidkhan
2 Replies

9. Shell Programming and Scripting

Order text display not correct.

My shell script below for import data to Oracle it run okay. but the text display not correct follow order command executed. =========================Shell Script code================= #!/bin/sh #directory = ${1-'pwd'} #run import data with SQLLoader runSQLLoader() { ... (2 Replies)
Discussion started by: raccsdl
2 Replies
Login or Register to Ask a Question