![]() |
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|