![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| xlC compilation error when dealing with operator overloading | luop0812 | AIX | 1 | 04-09-2008 04:09 PM |
| perl: When dealing with files that do not exist | joeyg | Shell Programming and Scripting | 2 | 02-20-2008 05:09 PM |
| tsch script problem (dealing with sed) | csnewbie84 | Shell Programming and Scripting | 10 | 05-02-2007 10:51 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Help in dealing with arra
I am readinga file lin by line and based craeting a arry of unique elemenst from the second column of the line. However when i coem out of the while loop my array becomes empty , can eny one tell me what I would be doing wrong
#!/bin/bash logfile="./mylog.dat" begin=100 end="$(( $begin + 1000 ))!d" index=0 Isthere=0 ENGINES="" sed "$begin, $end" $logfile | while read line do i=`echo $line | awk -F "," '{print $2}'` Isthere=0 for item in "${ENGINES[@]}" ; do if [ "$i" = "$item" ] ; then echo "Aleady there " Isthere=1 break fi done if [ "$Isthere" -eq 0 ] ; then echo "adding $Isthere" ENGINES[$index]=`expr $i` index=$(( $index + 1)) fi # echo $line done echo $index ${#ENGINES[@]} # howe ever at this point array is empty for i in "${ENGINES[@]}" ; do echo $i done |
|
|||||
|
Change your technique to get rid of the pipeline, change your shell to ksh, or live with the empty array. These are your options. Sorry, but you can't keep the pipeline, bash, and the array contents. One has to go. Here is a simple script to illustrate the problem...
Code:
$ cat script2
#! /usr/local/bin/bash
echo "cat
dog
mouse
rabbit
lion
wolf
dog
bat
lion
hamster
rabbit
elephant
elephant
whale
cricket" > list.txt
index=0
cat list.txt | while read item ; do
array1[index]=$item
((index=index+1))
done
echo array1: ${array1[@]}
exec < list.txt
index=0
while read item ; do
array2[index]=$item
((index=index+1))
done
echo array2: ${array2[@]}
exit 0
$ ./script2
array1:
array2: cat dog mouse rabbit lion wolf dog bat lion hamster rabbit elephant elephant whale cricket
$
|
|
||||
|
Fantastic , that was new to be , I thought of getting rid of the the "disk write" happening while out putting it in to a file.
sed -n "$begin, $end" $logfile >engines exec < engines while read line do # array insert done Thankyou very much , I am so used t bash such that , ksh is not easy for me to use now. It does not ahve the "auto completion when we type something and the the <tab> key stroke |