awk loop using array:wish to store array values from loop for use outside loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk loop using array:wish to store array values from loop for use outside loop
# 1  
Old 12-19-2013
awk loop using array:wish to store array values from loop for use outside loop

Here's my code:

Code:
awk -F '[ : ]'  'NR==FNR {
    if (/time/ && $5>10)
        A[++N]=$2" "$3":"$4":"($5-01)
    else if (/time/ && $5<01)
        A[++N]=$2" "$3":"$4-01":"(59-$5)
    else if (/time/ && $5<=10)
        A[++N]=$2" "$3":"$4":0"($5-01)
    else if (/close/) {
        B[FNR]=0
        n1=n2;
        n2=FNR;
    }
else if (/^sta EP/) {
match($0,/^sta EP([0-9]*)/,a)
snr=a[1] 
        if (snr != prev) {
            B[n1]=1
            B[n2]=1
        }
        prev=snr
    }
    next
}

FNR==1 {
    B[n1]=1; B[n2]=1
}
/time/ {
    M++
}
/close/ {
    if (B[FNR]) {
        $0=$1" ""EP"snr" ""12/31/2013 23:59:59"
done
    }
else if ((M%2) == 0 || (M%2) == 1) {
        NF=3
        $0=$0" "A[M+1]
    }
}
{print}' EAST_comprehensive2013_bfile.txt EAST_comprehensive2013_bfile.txt >> neen.txt

What I would like to do is store each iteration value of the variable snr currently defined as:

Code:
snr=a[1]

to be used in the following line:

Code:
$0=$1" ""EP"snr" ""12/31/2013 23:59:59"

So that when it goes through each of the matched ^sta EP values it uses the correct value of the loop to insert into the above line. Does this make sense? Currently, since I'm not sure how to store these values it only retains the last value of the loop, which makes sense because I'm not telling it otherwise. My question is how do I store that value and then call it later in the line above? 'a' is already an array from the match statement, but I'm not sure if I need to make snr also be an array, or what to do. Any help would be greatly appreciated! Thanks in advance!

Last edited by klane; 12-19-2013 at 02:17 PM.. Reason: didn't appear in message
# 2  
Old 12-19-2013
Please show us your sample input and expected output.
# 3  
Old 12-19-2013
I don't need any help modifying the rest of the awk script that I have posted, just the part of storing the variable. Basically it runs through a text file (example below) and for the last close statement for each station (EP*) it changes the close statement to

Code:
close EP00 12/31/2013 23:59:59

and for the next block of text for the next station it would then change the last close statement to:

Code:
close EP01 12/31/2013 23:59:59

etc, for all of the stations that are within the text file. I would like the variable 'snr' to retain the station name for each iteration through the loop, instead of only retaining the last station name, so that as it gets to the last close statement for each station it can use the correct station name.

Here's an excerpt from the text file below:
Code:
----------------------------------------- 
sta EP00  
time 10/23/2013 20:10:17 
sensor trillium_240_2 0 583 
add 
close sensor trillium_240_2 10/23/2013 20:10:17  

sensor trillium_120 0 279 
add 
close sensor trillium_120 10/23/2013 20:10:35
 --------------------------------------------- 
sta EP00 
time 10/28/2013 20:20:28 
sensor trillium_240_2 0 583 
add 
close sensor trillium_240_2 10/28/2013 20:20:28

sensor trillium_120 0 268 
add 
close sensor trillium_120 10/28/2013 20:20:45
 ---------------------------------------------- 
sta EP01
time 10/23/2013 20:10:17 
sensor trillium_240_2 0 583 
add 
close sensor trillium_240_2 10/23/2013 20:10:17  

sensor trillium_120 0 279 
add 
close sensor trillium_120 10/23/2013 20:10:35 
--------------------------------------------- 
sta EP01 
time 10/28/2013 20:20:28 
sensor trillium_240_2 0 585 
add 
close sensor trillium_240_2 10/28/2013 20:20:28

sensor trillium_120 0 280
add 
close sensor trillium_120 10/28/2013 20:20:28 
----------------------------------------------------------

It's important to note that there are more than two entries for each station, i.e., there can be up to 'n' entries, and one station block contains 2 close statements. For the sake of clarity I broke up each complete station block with the dashed lines.

The expected output would be to change the last station block's two close statements to read, if we used EP00 as the station for example

Code:
close EP00 12/31/2013 23:59:59

I had another question posted on another issue related to helping with this code, which have all been resolved from the above code example I have given. Now I just need to know how to store the value of 'snr' so I can use it in the last close statements for each station. Hopefully this helps clarify @akshay hegde

Last edited by klane; 12-19-2013 at 04:33 PM.. Reason: not formatted correctly
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

2. Shell Programming and Scripting

Convert values in an array using a loop

I have a headerless array of 1000 columns x 100000 rows. The array only contains 4 values; 0/0, 0/1, 1/1, ./. Here I am showing the 1st 3 rows and columns of the input array 0/0 0/0 1/1 0/1 0/1 0/1 0/0 ./. 0/0 0/0 0/0 0/0 I would like to convert the values in... (9 Replies)
Discussion started by: Geneanalyst
9 Replies

3. Shell Programming and Scripting

Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck. Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification. du -sh * | sort -rh 139G Dir_1 84G Dir_2 15G Dir_3 ls -l Dir_1 drwx------ 2... (3 Replies)
Discussion started by: 308002184
3 Replies

4. Shell Programming and Scripting

How to swap the values in array using for loop?

array=( 8 5 6 2 3 4 7 1 9 0 ) for i in "${array}" do echo $i done # i need the output like this by swapping of array values 0 9 1 7 4 3 2 6 5 8 (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

5. Shell Programming and Scripting

awk script: loop through array

I have a large file where I want to extract the data by using awk script. I have made a small sample of the input data. I have in the awk script two condition . The first one is to collect the initial time and the second one to collect the end time. I stored the difference between (Time=end-start)... (8 Replies)
Discussion started by: ENG_MOHD
8 Replies

6. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

7. Shell Programming and Scripting

awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk. '{nr=$2;f = $1} END{for (i=1;i<=f;i++) if (nr != i) print i, nr }' input1.csv >output1.csvinput1.csv 1 9 3 5 4 1 7 6 8 5 10 6 output1.csv > with the missing line number 6. 6 is... (5 Replies)
Discussion started by: sdf
5 Replies

8. Shell Programming and Scripting

Help with awk in array in while loop

Hi everyone:) I have 2 files - IN & OUT. Example: IN A:13:30 B:45:40 . . . UNLIMITED OUT Z:12:24 Y:20:15 . . . UNLIMITED I want first row of numbers of IN - OUT. Example 13-12 45-20 My code is (2 Replies)
Discussion started by: vincyoxy
2 Replies

9. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

10. Shell Programming and Scripting

How to use array values after the loop.

- I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop. Plz go through the script and suggest how... (1 Reply)
Discussion started by: Devesh5683
1 Replies
Login or Register to Ask a Question