How to get array to not split at spaces?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get array to not split at spaces?
# 1  
Old 07-19-2007
How to get array to not split at spaces?

I have been working on some code for a while, that will parse a log file, look for a specified time discrepancy between entries, and then print that line +/- n other lines out to a file...

Code:
#!/bin/bash

file=$1     # The input log file
maxTime=$2  # The time discrepancy to look for
n=$3        # The number of log entries to memorize
outFile=$4  # The output file

declare -a array

if [[ ! -n $1 || ! -n $2 || ! -n $3 || ! -n $4 ]]; then
    echo "ERROR"
    exit 128
fi

# This value is to print lines AFTER match
till=0

# This is a temp value to compare timestamps
previous=0

# egrep gets rid of all non-timestamped lines
egrep -n '^[0-9]' $file | \
while IFS="[.|:]" read number datetime min sec milli rest; do
    milliseconds=$(date -d "$datetime:$min:$sec" +%s)$milli
    line="$number:$datetime:$min:$sec.$milli|$rest"

    # Memorize the last n lines
    array=("${array[@]}" $line)
    if [[ ${#array[@]} -gt $n ]] ; then
        # get rid of overflow
        array=(${array[@]:1})
    fi

    # Calculate time difference on current and last line
    if [[ $previous -ne 0 ]] ; then
        (( difference = milliseconds - previous ))
    else
        difference=0
    fi

    if [[ $till -gt $number ]] ; then
        echo $line
    else
        if [[ $difference -gt $maxTime ]] ; then
            (( till = number + n ))

            # Print the history
            for item in ${array[@]}
            do
                echo $item
            done

            # Print the current match
            echo $line
        fi
    fi
    previous=$milliseconds
done

Can somebody tell me what is wrong here?

I think that the problem is that "$line" sometimes contains spaces in its text... which are then interpreted as separate array items... which fills up my buffer with parts of a single line instead of the memorized lines... but honestly I don't know what I can do about that
# 2  
Old 07-20-2007
Try to enclose the $line inside double quotes and use it like "$line"


Raghu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to split the string value to an array?

Test1.txt Tom is hot Test.sh filename="/directory/Test1.txt" set - A store while IFS= read value do awk '{split($value,store," ")}' done < "$filename" echo ${#sore} From the code in the executing file, I would like each... (8 Replies)
Discussion started by: TestKing
8 Replies

2. UNIX for Beginners Questions & Answers

How to split a string into array?

value=malayalam # i need to store the value in an array by splitting the character #the output i need is m a l a y a l a m Please use CODE tags for output data as well as required by forum rules! (5 Replies)
Discussion started by: Meeran Rizvi
5 Replies

3. Shell Programming and Scripting

Perl split and array

Hello, I have the following code: while ($line = <fd_in>) { 126 $line = " " . $line ; 127 print "our_line:$line\n"; 128 @list = split (/\s+/, $line) ; 129 print "after_split:@list\n"; 130 print "$list\t$list\t$list\t$list\t$list\t$list$list\t\n"; 131 $len =... (2 Replies)
Discussion started by: Zam_1234
2 Replies

4. Shell Programming and Scripting

split string into array in shell

Hi all, I want to split a string into array based on given delimiter, for example: String: "foo|bar|baz" with delimiter "|" into array: strArr to strArr with values foo, bar and baz. Thanks a lot. Roy987 (5 Replies)
Discussion started by: Roy987
5 Replies

5. Shell Programming and Scripting

spaces in array field

I have a file, names(i) where each entry is 'first last' name. 'cat names' is fine. But in a shell script >for file in $(cat names) > do > echo $file > done the first and last name appear on 2 lines. I have tried escaping and quoting the space but to no avail. The names are to be... (4 Replies)
Discussion started by: chuckmg
4 Replies

6. Shell Programming and Scripting

Split File of Number with spaces

How do i split a variable of numbers with spaces... for example echo "100 100 100 100" > temp.txt as the values can always change in temp.txt, i think it will be feasible to split the numbers in accordance to column. How is it possible to make it into $a $b $c $d? (3 Replies)
Discussion started by: dplate07
3 Replies

7. Shell Programming and Scripting

split and making an array inside another array

I want to run an awk split on a value that has been pushed through an array and I was wondering what the syntax should be?? e.g. running time strings through an array and trying to examine just minutes: 12:25:30 10:15:13 08:55:23 awk ' NR==FNR{ ... (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

array in ksh with elems containing spaces

Hi all, I've a .csv file containing data per line delimited with '|' (The fields may contains elements with spaces). e.g. (really a sample) ID|Name|fon 12345|Celal Dikici|+4921123456 12346|Celal Dikici Jun.|+4921123456 12347|Celal Dikici Sen.|+4921123456 12348|Celal|+4921123456... (3 Replies)
Discussion started by: Celald
3 Replies

9. UNIX for Dummies Questions & Answers

problem with the blank spaces in array

Hi all! i need your help. I'm getting started with this... in need to insert in an array string values. But the thing is that this strings have blank spaces... for example if a want to put "filed1 = " and "field2 = " .... in the array , when i want to print all the fields, it only shows... (4 Replies)
Discussion started by: kamicasse
4 Replies

10. Shell Programming and Scripting

split to array in perl

Collegues I have flat file in the following format. 137 (NNP Kerala) (NNP India) 92 (NN Rent) (NN Range) 70 (NNP Thiruvananthapuram) (NNP Kerala) 43 (NNP Tourist) (NNP Home) 40 (NNP Reserve) (NNP Now) 25 (SYM @) (NN hotelskerala) 25 (NNP Thiruvananthapuram-695001) (NNP Kerala) 23 (NN... (3 Replies)
Discussion started by: jaganadh
3 Replies
Login or Register to Ask a Question