patternbashMinor
Return the first number found greater than the provided input number (13 digits)
Viewed 0 times
numberthereturngreaterthandigitsprovidedinputfoundfirst
Problem
This script converts the numbers to be at least 13 characters long (for UNIX_MS strings). For use with timestamps, I'm having issues with it being very slow. I wanted an alternative to grepping for one specific
For the output I wanted the line number in the file (for slicing) as well as the original line (to confirm/inspect).
I'm very specifically looking for optimizations as I'd like this to be as-close-to-as-fast as grepping a single timestamp.
Usage:
UNIX_MS timestamp and not finding it and then having to grep multiple more times.For the output I wanted the line number in the file (for slicing) as well as the original line (to confirm/inspect).
I'm very specifically looking for optimizations as I'd like this to be as-close-to-as-fast as grepping a single timestamp.
Usage:
./script.sh file UNIX_MS#! /bin/bash
# return the first number found that's greater than the provided input number
res=
linenum=
count=0
returnline= # hold onto the line for return
tocheck=$2
tocheck=$(($tocheck*(10**(( ${#tocheck} - 13) * -1))))
inseconds=$(($tocheck/1000))
date=$(date -r $inseconds)
echo "Looking for first timestamp -ge to $date.."
while read line;
do
count=$(($count + 1))
timearray=$(grep -o -E "^(.*?)([0-9]{10,13})?" = $tocheck?"
linenum=$([ "$timestamp" -ge "$tocheck" ] && echo $count)
if [ -z "$linenum" ]; then
:;
else
returnline=$line
break;
fi
done < $1
echo "$linenum:$returnline"
echo ""Solution
I'm having issues with it being very slow.
What makes your script code slow is that you're reading in the file yourself with the
No matter what you want to search for with
For the output I wanted the line number in the file (for slicing) as well as the original line (to confirm/inspect).
Prefix each line of output with the line number within its input file.
you simply can do what you want using this option.
Thus you can get rid of your
What makes your script code slow is that you're reading in the file yourself with the
while loop, and apply grep to each single input line, instead of passing grep the file itself and let it just do it's job.No matter what you want to search for with
grep, you should always first pass your input to it with a single call, and inspect the results afterwards.For the output I wanted the line number in the file (for slicing) as well as the original line (to confirm/inspect).
grep already has this feature intrinsically (at least as it says from this documentation):-n, --line-numberPrefix each line of output with the line number within its input file.
you simply can do what you want using this option.
Thus you can get rid of your
while loop and count variable to determine the line number yourself.Context
StackExchange Code Review Q#112898, answer score: 4
Revisions (0)
No revisions yet.