Using array commands in 2 files in the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using array commands in 2 files in the same time
# 1  
Old 11-12-2012
comparing values in 2 files (awk array)

Hi bodies.
You think is it possible to make arrays from 2 different files in a same time and then for example compare the values in these 2 arrays or any other instruction
for example, I have:
Code:
 
38558   29   31
 38557   28   31
 38556   23   31
 38555   08   31
 38554   08   28
 38553   21   31
 38552   21   28
 38551   01   28
 38549   25   31
 38548   25   28

in first file and also I have
Code:
 32782   28   18
 32783   02   18
 32784   01   18
 32785   29   18
 32786   25   23
 32787   25   18
 32788   00   18
 32789   25   26
 32790   02   23
 32791   29   26
 32792   23   26
 32793   01   23
 32794   28   23

in second file
and I'm going to do something like:
Code:
BEGIN{print"//start";P=0}

{X1[NR]=$1;Y1[NR]=$2;Z1[NR]=$3;}
{X2[NR]=$1;Y2[NR]=$2;Z2[NR]=$3;}

and then compare for example Y1[3] & Y2[2]
Thanks for your help

Last edited by Behrouzx77; 11-12-2012 at 01:09 PM..
# 2  
Old 11-12-2012
try:
Code:
awk '
BEGIN{print"//start";P=0}
 
{X1[NR]=$1;Y1[NR]=$2;Z1[NR]=$3; getline < f;
 X2[NR]=$1;Y2[NR]=$2;Z2[NR]=$3;}
' f=file2 file1

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 11-13-2012
Thanks rdrtx1

another question is if for example I need to refer to length of these arrays in the body what should I do?(I mean NR of each array)

Code:
 for (j=0 ; j<=NR2 ;j++){
 for (i=0 ; i<=NR1;i++)

with this syntax I face error and AWK does not know NR1 & NR2.
How can I address NR1 & NR2?
# 4  
Old 11-13-2012
In the example, file1 NR is the only NR. So NR is the same for both arrays.
# 5  
Old 11-13-2012
You mean I can't have 2 different NR even if I have 2 files with different sizes?
# 6  
Old 11-13-2012
Not with this example solution of reading line by line of each file at the same time.
# 7  
Old 11-13-2012
Well, try this:
Code:
awk 'FNR==NR {X1[FNR]=$1;NR1=FNR;next}
     {X2[FNR]=$1}
     END {for (i=1;i<=NR1;i++) print  "X1: ", X1[i],",\tX2: ", X2[i], i; for (i=NR1+1;i<=FNR;i++) print "X1:\t,\tX2: ", X2[i], i}
    ' file1 file2
X1:  38558 ,    X2:  32782 1
X1:  38557 ,    X2:  32783 2
X1:  38556 ,    X2:  32784 3
X1:  38555 ,    X2:  32785 4
X1:  38554 ,    X2:  32786 5
X1:  38553 ,    X2:  32787 6
X1:  38552 ,    X2:  32788 7
X1:  38551 ,    X2:  32789 8
X1:  38549 ,    X2:  32790 9
X1:  38548 ,    X2:  32791 10
X1:        ,    X2:  32792 11
X1:        ,    X2:  32793 12
X1:        ,    X2:  32794 13

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To run 5 commands at the same time with process from a list

I have many command is list in the variable lists,each command will run a very long time, so I want to run 5 commands at the same time with process till it complete run all the command, lists="aa bb cc dd xx gg blabla zz ......." ( a very long list) can some one point me the codes? ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

How to time stamp executed commands?

Hi guys, I am executing a pretty long ksh script and need to time stamp every command which runs inside. Unfortunatly 'echo date' is not the option here. May be someone knows another way or utility which can be used to log executed command and timestamp next to it. Thanks PS I work in ksh88 (4 Replies)
Discussion started by: aoussenko
4 Replies

3. UNIX for Dummies Questions & Answers

Effective Commands To Get Time/Size in Bash

Hello All, Is there an effective approach in Bash to get size of the file and time stamp(including year) on the file. I have below please comment. SIZE=`/usr/bin/du -sh "${LOCATION}"/"${DATAFILE}" | awk '{print $1}'` BSIZE=`/usr/bin/du -b "${LOCATION}"/"${DATAFILE}" | awk... (10 Replies)
Discussion started by: Ariean
10 Replies

4. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

5. Shell Programming and Scripting

Script to run commands at a specific time.

Hello All, I have written a script which which is working fine to a certain logic of it. But i want a part of the script to run two commands at 00:10 hrs every day. These two command are 1. rm -rf /path/to/folder 2. mail the content of a file. How do i achieve this. Thanks. ... (4 Replies)
Discussion started by: Siddheshk
4 Replies

6. Ubuntu

How to get columns TIME and TTY of commands ps -A?

Hi, Commands ps -A include four parameters are PID, TTY, TIME and CMD. I can not found pathnames of TTY and TIME which I can read from file in C language to get information display on screen. Thank you! Ex: PID TTY TIME CMD 1 ? 00:00:01 init (2 Replies)
Discussion started by: newbie_member
2 Replies

7. Shell Programming and Scripting

Execute 2 Commands at the same time

Hi @all I have got the following problem: I want my Master-Script to execute 2 Sub-scripts at the same time. How can i realize that? Thx for your help Greez Roger (2 Replies)
Discussion started by: DarkSwiss
2 Replies

8. UNIX for Dummies Questions & Answers

multiple commands to be executed at the same time

how to execute more than one command at the same time in unix .. (4 Replies)
Discussion started by: hemaa
4 Replies

9. Shell Programming and Scripting

Run several commands at a time

Hello guys, I am new at shell scripting and I want to create a script that runs several commands at a time, ie: uptime, w, df -h and so on and send the output of this commands to a text file so it can be send via email at a certain time using crontab. Any help will be much appreciated! (4 Replies)
Discussion started by: agasamapetilon
4 Replies

10. Shell Programming and Scripting

how to get status array for the commands in eval

I want to get a status code array for the commands in eval. For example, eval "ls abc; ls def; ls $HOME" I want to get the status code for each ls command in eval. Is there any way to make it? Thanks. (2 Replies)
Discussion started by: pankai
2 Replies
Login or Register to Ask a Question