The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
array in ksh with elems containing spaces Celald Shell Programming and Scripting 3 05-28-2008 01:45 AM
split variable values into array finalight Shell Programming and Scripting 4 05-21-2008 03:21 AM
Access value outside awk or split value of array jason.bean UNIX for Dummies Questions & Answers 1 11-26-2007 04:33 PM
[KSH] Split string into array piooooter Shell Programming and Scripting 3 09-01-2007 12:22 PM
split to array in perl jaganadh Shell Programming and Scripting 3 07-06-2007 05:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-19-2007
jjinno jjinno is offline
Registered User
  
 

Join Date: Jul 2007
Location: Cloud 9
Posts: 72
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 (permalink)  
Old 07-20-2007
Raghuram.P Raghuram.P is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 44
Try to enclose the $line inside double quotes and use it like "$line"


Raghu
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0