How to solve the problem of overwriting an array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to solve the problem of overwriting an array?
# 1  
Old 11-01-2011
How to solve the problem of overwriting an array?

Hi all,

I have a file.....

Quote:
aws 21 34 678960
fsd 78 56 689754
xsa 55 77 764292
klj 32 54 567873
I want to print 2nd column arranged according to order of first column, present in second file.....

Quote:
xsa
fsd
klj
sdf
aws
So, the output should be:

Quote:
55
78
32
ABSENT
21
I am using following code:

Code:
awk 'NR==FNR{a[$1]=$2;next}{print a[$0]?a[$0]:"ABSENT\t"}' file1 file2

But, it seems that the array created is overwriting the values one by one and finally printing only the last row.
The output Iam getting is like:

Quote:
ABSENT
ABSENT
ABSENT
ABSENT
21
Can anyone please help me in figuring out this problem ........
# 2  
Old 11-01-2011
There are probably spaces in the second file, try this:
Code:
awk 'NR==FNR{a[$1]=$2;next}{print a[$1]?a[$1]:"ABSENT"}' file1 file2

# 3  
Old 11-01-2011
Hi Franklin,
Thanks.....

but ur code is also giving d same results......

ABSENT
ABSENT
ABSENT
ABSENT
21

Can u Please find some other way.....
# 4  
Old 11-01-2011
Code:
awk 'NR==FNR{a[$1]=$2;next}{print a[$1]?a[$1]:"ABSENT"}'  file1 file2

Code:
[orange@efcgnis ~/aen/temp]cat file1
aws 21 34 678960
fsd 78 56 689754
xsa 55 77 764292
klj 32 54 567873
[orange@efcgnis ~/aen/temp]
[orange@efcgnis ~/aen/temp]cat file2
xsa
fsd
klj
sdf
aws
[orange@efcgnis ~/aen/temp]
[orange@efcgnis ~/aen/temp]awk 'NR==FNR{a[$1]=$2;next}{print a[$1]?a[$1]:"ABSENT"}' file1 file2
55
78
32
ABSENT
21

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me solve this scripting problem please

Hello, I would really appreciate some help into approaching this problem: - i have a random txt file with x lines and y rows following this pattern: ex: ip1 | user1 | command ip2 | user2 | command ip3 | user3 | command - i need to telnet/ssh into these ip's, login with... (7 Replies)
Discussion started by: catalinstk
7 Replies

2. Shell Programming and Scripting

Unknown Problem. I really want your help to solve this!

Take a look on this code: #!/bin/sh currentpath=`pwd` if ; then #Normal user if ; then "$currentpath"/.cleaner else ./runit fi else #Root user if ; then rm -r /some fi mkdir /some cd /home/ echo "`ls --group-directories-first -1`" > /some/allusers cat /some/allusers | sed 's/... (17 Replies)
Discussion started by: hakermania
17 Replies

3. UNIX for Advanced & Expert Users

how would you solve this problem?

I have a file process.txt I wanted to just grab data in "process" column. Name process process_id status Adminserver adminserver 22669 Running Browser Engine browserengine ... (7 Replies)
Discussion started by: soemac
7 Replies

4. IP Networking

Need to solve complex network problem

I have a Red Hat linux server X on a x.x.0.0 network. This machine also has to communicate with another server Y on a network called y.y.0.0 Server X has two network interfaces. eth0 is configured on the x.x.0.0 network and has a default gateway on the x.x.0.0 network. In order to... (4 Replies)
Discussion started by: soliberus
4 Replies

5. UNIX for Advanced & Expert Users

How to solve restarting problem

Hi! My unix os version is OSF1 CP1 V4.0 878 alpha. It startup normally but it restarts within 5 sec. I would like to know how to solve . Please reply to me. Thanks . akzin (2 Replies)
Discussion started by: akzin
2 Replies

6. UNIX for Advanced & Expert Users

use two unix commands to solve the following problem

Hi, all, The following commands could compute the 10 most frequent bigrams from a input sequence which is in a file infile. I would like to know whether there is somebody who can use only two unix commands to do the same work. -------------------- tr " " "\012*" <infile >out1 tail +2... (3 Replies)
Discussion started by: vicky20000
3 Replies

7. Programming

Can any one solve this Problem...!!!

Try to solve this.....It's a nice program..... #include<stdio.h> void change() { /*Write something in this function so that the output of printf in main function should give 5 . Do not change the main function */ } void main() { int i=5; change(); (9 Replies)
Discussion started by: Baba B. Saheb
9 Replies

8. UNIX for Advanced & Expert Users

can't solve that problem [PLEASE HELP]

well, my internet brakes down every day because of my server, i don't have troubles with RAM or anything i think... that problem started since i am running an unrealircd server... well, my internet brakes down and when i try to access the inside ip from the server on http port 80, it says that:... (2 Replies)
Discussion started by: AiRkO
2 Replies

9. Programming

How can I solve this problem?

I'm now designing a server application which can serve large number of clients' request. I've a question to ask, that is, main process will block when invoke the "accept" function, if a client request comes, main process should be separated into 2 processes by invoking "fork" function, the parent... (4 Replies)
Discussion started by: acqy
4 Replies
Login or Register to Ask a Question