Help with awk in array in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with awk in array in while loop
# 1  
Old 03-19-2009
Error Help with awk in array in while loop

Hi everyoneSmilie
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

#!/bin/csh -f
set y=1
while ( $y<UNLIMITED )
set in = `awk -F':' '{print $2}' IN |awk '{array[NR]=$0} END {print array[y];}'`
set out = `awk -F':' '{print $2}' OUT |awk '{array[NR]=$0} END {print array[y];}'`
set number = `expr $in - $out`
echo "$number"
@ y = $y + 1
end

expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
all is expr: syntax error
I tried to solve more than one day already.Smilie And also duno wat should i write instead of UNLIMITED.
Anyone can help me?
# 2  
Old 03-19-2009
Error

As a starting point, you may want to review the following, https://www.unix.com/shell-programmin...#post302298986

Files are read into a two-dimensional array and then processed for printing.


The original query was to extract the 5th column from multiple files and print them out in columnar format.
# 3  
Old 03-19-2009
Quote:
Originally Posted by vincyoxy
Hi everyoneSmilie
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

Please put code inside [code] tags.
Quote:
Code:
 
#!/bin/csh -f

csh is not recommended for scripting.
Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:
Code:
set y=1
while ( $y<UNLIMITED )
set in = `awk -F':' '{print $2}' IN |awk '{array[NR]=$0} END {print array[y];}'`
set out = `awk -F':' '{print $2}' OUT |awk '{array[NR]=$0} END {print array[y];}'`
set number = `expr $in - $out`
echo "$number"
@ y = $y + 1
end


Code:
cut -d: -f2 in > in.tmp
cut -d: -f2 out > out.tmp
paste -d- in.tmp out.tmp
rm in.tmp out.tmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. Shell Programming and Scripting

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

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. 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

4. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Array with do while and if loop

Hi All, I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array. So far I can read the array, but I can't get the if statement to work. Any suggestions... (5 Replies)
Discussion started by: nitin
5 Replies

8. Shell Programming and Scripting

Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem? Here I prepared a sample for you to see my problem; zgrw@Rain:~$ cat test asd 123... (4 Replies)
Discussion started by: zgrw
4 Replies

9. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 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