Issue with the incorrect number of array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with the incorrect number of array elements
# 1  
Old 10-13-2015
Issue with the incorrect number of array elements

Hello ,

I have a file : RestartSession.txt with the below contents :

Code:
Backup  p203pcrw01_OS_Weekly  Failed          full    10/11/2015 10:00:07 PM  1444572007      10/11/2015 10:26:23 PM  1444573583      0:00    0:26    18.76   1      08       0       0       0       2       2       180668  100%    root.root@xdfgrtewses  2015/10/11-331
Backup  p014pdft01_App_Weekly Failed          full    10/12/2015 12:00:04 AM  1444579204      10/12/2015 02:28:42 AM  1444588122      0:00    2:28    247.33  1      00       0       0       0       1       1       189950  100%    root.root@ xdfgrtewses 2015/10/12-3

However when I am trying to check for the number of array elements and the last field as shown below I am not getting correct result

Code:
$ while read line
> do
> IFS='       ' read arr <<< "$line"
> lastIndex=${#arr[@]}
> echo $lastIndex
> val= expr $lastIndex - 1
> echo ${arr[@]:-1}
> Sid=${arr[@]:-1}
> done < RestartSession.txt
1
0
Backup  p203pcrw01_OS_Weekly  Failed          full    10/11/2015 10:00:07 PM  1444572007      10/11/2015 10:26:23 PM  1444573583      0:00    0:26    18.76   1      08       0       0       0       2       2       180668  100%    root.root@xdfgrtewses  2015/10/11-331
1
0
Backup  p014pdft01_App_Weekly Failed          full    10/12/2015 12:00:04 AM  1444579204      10/12/2015 02:28:42 AM  1444588122      0:00    2:28    247.33  1      00       0       0       0       1       1       189950  100%    root.root@ xdfgrtewses 2015/10/12-3
$

Could someone please help me.

Thanks
Rahul
# 2  
Old 10-13-2015
Try read -ain bash or read -A in ksh93 or zsh.



--
Also:
Code:
IFS='       '

probably means something different than what you had in mind:

In bash it is equivalent to IFS=' ' (1 space), which gets interpreted as "one or more spaces"

In ksh93 and zsh:

- if it has an even number of spaces it is equivalent to
Code:
IFS='  '

(two spaces), which gets interpreted as one single space, i.e. each space delimits a field.

- if it has an odd number of spaces it is equivalent to
Code:
IFS=' '

(1 space), which gets interpreted as "one or more spaces"

Last edited by Scrutinizer; 10-13-2015 at 09:57 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-13-2015
And if you do nothing else with $line then you can replace
Code:
while read line
do
  read -a arr <<< "$line"

by
Code:
while read -a arr
do

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 10-13-2015
The solution depends on the file structure, i.e. the field delimiters. Should those be <TAB>s try while IFS=' ' read -a arr; do ..., and e.g. the two datetime fields will be preserved. If they are one or more spaces, the datetime fields will be split up and the field count changes.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-13-2015
Hello ,

I am using AWS Redhat server build recently not sure of the version. I tried read -a but it didnt worked but when I tried read -A it worked fine. Also I am not using the following version of code which is working fine.

Code:
while read line                                              ########Find all the Session ID's of the Jobs from the above List
do
    IFS=' ' read -A arr <<< "$line"
    lastIndex=${#arr[@]}
    echo $lastIndex
    val= expr $lastIndex - 1
    Sid=${arr[${#arr[@]} - 1]}
    echo $Sid
 done < $FailFile

Thanks a lot for providing indepth details and help.

Rahul
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 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. Shell Programming and Scripting

Random elements from array

Hi I wanted to print random elements from an array at bash shell I use the following code, but I always see first element getting printed #!/bin/bash c=1 expressions=(pink red white yellow purple) while ]; do echo "The value of RANDOM is $RANDOM" selectedexpression=${expressions}]};... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

4. Programming

Array Elements Check

Hi everyone, :) I'm trying to make a simple C program that scans an array of chars to see if its elements are similar. I can't understand what's wrong. Could you help me to fix this? Here is the code. Thanks! #include<stdio.h> int main() { int arr; int i, len; int flag =... (10 Replies)
Discussion started by: IgorGest
10 Replies

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

6. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

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

8. Shell Programming and Scripting

eval, array, number of elements - couldn't get it

I try to get a number of elements in an array, using dynamic array name. I need the array name to be dynamic. The array name is constructed as 'inf_ln_$nmb', where $nmb is a file line number So, say I have the arr 'inf_ln_4': > for (( el=0; el<${#inf_ln_4}; el++ )); do > echo "$el:... (1 Reply)
Discussion started by: alex_5161
1 Replies

9. UNIX for Dummies Questions & Answers

incorrect array values

I have a file with 2 lines and 3 filelds -bash-3.00$ cat del_old_files.cfg $DBA_WORK_DIR *sybdba* 30 $DBA_LOG_DIR *sybdba* 30 I have tried to set up arrays for each field (bash shell) declare -a dirArray=`cut -d' ' -f1 < del_old_files.cfg` declare -a nameArray=`cut -d' ' -f2 <... (6 Replies)
Discussion started by: jhillier
6 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