Print arguments using array elements not working?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print arguments using array elements not working?
# 8  
Old 05-06-2013
Again you have showed a broken shell script and not the data you want to read. Show that.
# 9  
Old 05-06-2013
Ok data to be read is as follows:
Code:
3701 09 39 51.42 85 22 48.02  15.544
3800 09 35 45.07 85 14 19.24  15.908
3825 09 44 39.06 85 12 04.66  16.113
3829 09 39 52.06 85 11 44.99  16.252
3935 10 16 09.40 85 22 22.69  16.246
3942 10 02 09.58 85 21 58.22  14.464
3977 10 08 41.28 85 19 20.60  16.165
3978 09 54 39.79 85 19 16.53  15.980
3979 10 19 42.93 85 19 12.80  16.170
4006 09 52 01.37 85 18 18.69  14.936
4014 09 57 00.09 85 17 49.63  16.231
4063 10 14 22.03 85 14 37.34  15.716
4065 10 11 55.20 85 14 32.15  15.470
4075 09 52 56.26 85 13 41.39  15.698
4119 10 20 50.27 85 09 55.13  15.772
4163 10 51 58.58 85 26 10.99  15.572
4177 10 53 05.07 85 25 04.96  15.344
4200 10 49 28.47 85 23 02.22  14.927
4228 10 35 44.83 85 20 33.71  13.942
4240 11 00 20.23 85 19 56.52  15.988
4248 10 27 30.88 85 19 14.33  15.848
4250 10 44 38.15 85 19 06.81  15.830
4254 10 37 33.60 85 18 38.93  15.816
4257 10 58 11.40 85 18 35.19  14.914
4267 10 58 58.07 85 18 07.43  15.335
4268 10 52 53.96 85 17 57.84  14.916
4275 10 36 13.78 85 17 37.21  15.784
4280 10 49 24.59 85 17 25.13  14.922
4290 10 31 37.94 85 16 47.25  14.174
4292 10 30 12.11 85 16 25.06  14.368
4294 10 36 23.12 85 16 22.04  12.945
4304 10 27 38.45 85 15 46.72  16.144
4306 10 35 08.85 85 15 39.47  13.347
4317 10 40 42.49 85 14 55.52  15.492
4318 10 59 32.23 85 14 55.08  15.861
4322 10 47 52.54 85 14 44.51  15.998
4328 11 00 27.06 85 14 22.76  15.728
4331 10 59 16.44 85 14 18.55  15.603
4336 10 59 31.97 85 13 50.87  16.098
4341 10 43 18.75 85 13 38.87  15.256
4343 10 58 19.09 85 13 30.98  15.727
4346 10 38 48.96 85 13 25.63  13.621
4364 10 47 29.13 85 12 24.19  15.029
4372 10 32 03.89 85 12 00.92  15.278
4373 10 42 09.04 85 11 46.83  15.508
4375 11 00 43.06 85 11 42.25  15.139
4376 10 46 19.58 85 11 36.34  16.491
4379 10 59 28.37 85 11 29.69  15.888
4380 10 46 13.56 85 11 24.23  16.097
4451 11 34 01.78 85 26 05.47  15.762

I have only included 8 columns of 46 but it's just numerical data organized into columns, so the choices[] can be an array of length 1 to 8 depending on the chosen value of num.

---------- Post updated at 12:01 PM ---------- Previous update was at 11:43 AM ----------

Also you might mention that I don't need to define the array for the column choices since I can just put it in the for loop that writes out the columns and re-define it each time, but I would like to keep it the way it is in case I need to do more things with this script later on that might involve the definition of the column numbers.
# 10  
Old 05-06-2013
If you have BASH:

Code:
while read -a array
do
...
done < inputfile

If you don't:

Code:
while read -r LINE
do
        set -a array $LINE
done

# 11  
Old 05-06-2013
Quote:
Originally Posted by Corona688
If you have BASH:

Code:
while read -a array
do
...
done < inputfile

If you don't:

Code:
while read -r LINE
do
        set -a array $LINE
done

You mentioned something like this before but I'm not sure where in my code this belongs. What is the "..." in the while? Sorry for apparently being an idiot.
# 12  
Old 05-06-2013
I mentioned something like it before, but bash has an -a option to read whole arrays instead of a list of variables.

In KSH, you can split one biga rray instead.

Quote:
Originally Posted by shaner
What is the "..." in the while?
Whatever you want.

Code:
while read -a array
do
        echo "Column 3 is ${array[2]}"
done < inputfile

# 13  
Old 05-06-2013
Quote:
Originally Posted by Corona688
I mentioned something like it before, but bash has an -a option to read whole arrays instead of a list of variables.

In KSH, you can split one biga rray instead.

Whatever you want.

Code:
while read -a array
do
        echo "Column 3 is ${array[2]}"
done < inputfile

Can you indicate how this solves my problem? Everything seems to be working fine with my script except for the print command that doesn't want to print the columns that I give it...
# 14  
Old 05-06-2013
Hard to say when I can't even tell what you're trying to do... Why are you using getline for one thing? awk reads lines by itself...

You want a complete script? Fine.

Code:
printf "Enter a list of columns separated by spaces: "
read COLS

awk -v COLS="$COLS" 'BEGIN { L=split(COLS,C); }

# Only run this for lines 4 and up
NR>3 {
        STR=""
        for(N=1; N<=L; N++) STR=STR " " $(C[N]+0)
        $0=STR
} 1' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Select / Print odd-even elements of an array

Hello experts, I wish to print the contents of odd-even numbered indices of an array. The problem statement is as follows : 1. The first line contains an integer, (the number of test cases). 2. Each line of the subsequent lines containing a string. Example: 2 Haider Bash ... (4 Replies)
Discussion started by: H squared
4 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. UNIX for Dummies Questions & Answers

How to declare an array in UNIX and print the elements with tab delimits?

Hello, In a shell script, I want to declare an array and subsequently print the elements with tab delimits. My array has the following structure and arbitrary elements: myArray=('fgh' 'ijk' 'xyz' 'abc'); Next, I would like to print it with a '\n' at the end. Thanks for your input! ... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

4. Shell Programming and Scripting

Removing elements from an array

Hi I have two arrays : @arcb= (450,625,720,645); @arca=(625,645); I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720). Can anyone sugget me how to perform this operation? The code I have used is this : my @arcb=... (3 Replies)
Discussion started by: rkrish
3 Replies

5. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

6. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

7. Shell Programming and Scripting

Store all the passed arguments in an array and display the array

Hi I want to write a script which store all the parameters passed to the script into an array. Once it is stored I want scan through the array and and delete those files for last month present inside the directory. The files in directory is appneded with YYYY_MM_DD. I want to know how can I... (3 Replies)
Discussion started by: dgmm
3 Replies

8. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

9. UNIX for Dummies Questions & Answers

Deleting Array Elements

Hi, I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as... (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies
Login or Register to Ask a Question