Help in dealing with arra


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in dealing with arra
# 1  
Old 08-27-2007
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
# 2  
Old 08-28-2007
Put a few lines of data in a file called test.file. Try this loop style:
cat test.file | while read line
and the array will be empty after the loop ends. Then try:
exec < test.file
while read line
and the array will have data after the loop ends. The while loop is being placed in a subshell if it is in a pipeline. ksh will not do that but other shells do.
# 3  
Old 08-28-2007
You mean I ahve to change the below line
sed "$begin, $end" $logfile | while read line

to
exec < test.file
while read line

But I do not want to read file fully , but only from begin to end.
# 4  
Old 08-28-2007
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
$

ksh is the only shell I know that will populate both arrays. This is one of the reasons that I strongly prefer ksh to other shells. (The other is ksh co-processes.) So my suggestion: switch to ksh.
# 5  
Old 08-28-2007
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
# 6  
Old 08-28-2007
You can use bash as your interactive shell and still write scripts in ksh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dealing with edge-list

I have an edge-list with nodes, edge.txt A B B J J H C A G H G A A C K G I have another file which tells me which of these nodes are important, input.txt G C A (3 Replies)
Discussion started by: Sanchari
3 Replies

2. UNIX for Dummies Questions & Answers

Dealing with sum

I have file input 1/1/2013 1AS030A 0 1083 CHINA 1/1/2013 1AS030B 0 675 KOREA 1/1/2013 1AS035A 162 662 CHINA 1/1/2013 1AS035B 51 799 INDIA 1/1/2013 1AS035C 0 731 CHINA 1/2/2013 1AS073A 10 1375 KOREA... (5 Replies)
Discussion started by: radius
5 Replies

3. Slackware

Dealing with lilo and uefi

Recently I have been trying to boot into slackware on a new laptop that came preinstalled with windows 8. I have successfully installed slackware and Lilo, but I have had great difficult attempting to boot into it. Since the laptop contains no optical drive, I have been attempting to boot into... (2 Replies)
Discussion started by: a sandwhich
2 Replies

4. Shell Programming and Scripting

Dealing with multiple files

Korn Shell I have hundreds of small files like below created every day. A midnight cron job moves them to the location /u04/temp/logs But sometimes I have to manually move these files based a certain dates or time. I have two basic requirements 1.Using mv command I want to move all .dat... (2 Replies)
Discussion started by: kraljic
2 Replies

5. UNIX and Linux Applications

Dealing with geany core

Geany : Home Page, the text editor, sometimes crashes and leaves a geany.core file. This is a binary file and supposedly contains all unsaved work and possibly some other information. Does anyone know how to deal with this file? (2 Replies)
Discussion started by: figaro
2 Replies

6. Programming

Need help with Card Dealing Program

I'm currently making a card dealing program, it is suppose to display a list of cards like this: "Ace of Heart, is red" "Two of Heart, is red" . . "Ace of Spade, is black" and so on for all suits and numbers. here is my current code: #include <stdio.h> #include <stdlib.h> #include... (3 Replies)
Discussion started by: Izzy123
3 Replies

7. Shell Programming and Scripting

cp not dealing with variable properly? please help

I am having trouble with a script that is supposed to : a)take all the jpg pictures in a given directory/parameter and create thumbnails of it in a directory on the desktop. e.g from /here/are/the/files.jpg to ~/Desktop/parser-the/files.png this is my script: all the individual parts... (2 Replies)
Discussion started by: orochinagi
2 Replies

8. Shell Programming and Scripting

Dealing with files with spaces in the name

Hello, I'm a computer science major and I'm having problems dealing with file names with spaces in them. Particularly I'm saving a file name in a variable and then using the variable in a compare function i.e. a='te xt.txt' b='file2.txt' cmp $a $b If anyone could help me with this particular... (10 Replies)
Discussion started by: jakethegreycat
10 Replies

9. UNIX and Linux Applications

webinject - dealing with popups

Hello We have a webapp that launches a link in a popup. We need to use webinject to check the content of that popup but have been so far unsuccessful. We use webinject as a nagios plugin. Does anyone have any experience of using/configuring webinject and know if this is possible or not? ... (1 Reply)
Discussion started by: skewbie
1 Replies

10. Shell Programming and Scripting

Dealing with log files

Hi , My requirement is that i need to search for a number of strings in a log file and print them with line numbers.The search should be date wise. The sample log file is : Jan 17 02:45:34 srim6165 MQSIv500: (UKBRKR1P_B.LZ_ BENCHMARKS)BIP2648E: Message backed out to a queue; node... (6 Replies)
Discussion started by: charudpss
6 Replies
Login or Register to Ask a Question