where values will be there from 00:00 hrs to 23:59 hrs. I'm matching for last occurance of 23:59 and printing 1440 lines (grep -B1440 "23:59").. But if values are missing from 00:00 hrs to 2:00 hrs... previous day values also gets printed.
How to avoid this prob???
Last edited by Scott; 07-29-2011 at 06:49 AM..
Reason: Code tags
Well, you have an unreliable data set, so you need to analyze it without any speific time. You could use awk or shell to find each pair of lines when time folds back, an apparent day boundary. (Some like to log the date like "YYYY-MM-DD_HH:MM:SS ($$) ...." in every line, so this situation is prevented.) This finds the boundary lines:
Code:
lntm=0
while read tm pval
do
xtm=${tm#*:}
ntm=${tm%%:*}${xtm%:*}${xtm#*:} # make time tm look like an integer ntm
if (( lntm > ntm )) # is last numeric time greater (fold back)?
then
echo $ltm $lpval
echo ----
echo $tm $pval
echo . . .
fi
lntm=ntm ltm=tm lpval=pval
done <$in_file
Last edited by DGPickett; 07-29-2011 at 02:51 PM..
pval is a shell variable, payload value. read puts values in variables, using the shell's usual $IFS characters as separators, and putting excess values, at least with ksh but it varies, in the last variable, so "read a b" is read first field into $a and the rest into $b. My personal mental nomenclature is that record fields are, at any given oment, either keys (needed to address this record) or payload (stuff that serves the application but not this support function, along for the ride. The key is part of the delivery mechanism, like the space shuttle, and the rest is cargo, goes in the bay to follow the key to the destination.
Now, an alphanumeric compare can compare times without removing the :, but I was lazy and had not gotten into researching string compare, a sin in the Internet Age, so now we both learn. It seems "[[ $a < $b ]]" works fine, especially with such bland strings. Depending on shell and maybe locale, '[[ "A" < "A" ]]' and '[[ "3" < "c" ]]' might not be what yoou think. The shell has several parallel comparison suites:
'[...]' is 'man test' -- while often built-in, for old sh, '[' is /bin/[, a link to /bin/test,
'[[ ... ]]' is 'man ksh - Conditional Expressions' does string comparison,
(( ... )) is 'man ksh - Arithmetic evaluation' with C-like operators, double precision floating point arithmetic or long double precision floating point, supports nice things like variable without '$' and '+=' but unless bash not '++'.
So, the narrative flow is:
Prime the last numeric time value lntm
Start a 'while read' loop, reads lines until EOF into variable(s), sometimes losing some white space, but a very useful shell bit for low latency, no-upper-list-size-limit, one line at a time processing.
xtm is an intermediate variable, as the ksh $var slicers #, ##, %, %% are a bit crude.
I manke numeric time ntm by concatenation of substrings of tm and xtm. Maybe I could have used a numeric substring slicer, like bash and ksh93 have '${var:start:len}', since the time strings are well behaved. If they were 1-2 digit variable width, I would need to multiply and add them, not concatenate.
If the last numeric time is greater, this is a day boundary.
then spit out a reconstituted last line and new line with divider and elipsis.
load the current line variables as last line variables
loop back. Redirect stdin to input file.
Revised as noted for string compare:
Code:
ltm=0
while read tm pval
do
if [[ $ltm > $ntm ]] # is last time greater (fold back)?
then
echo $ltm $lpval
echo ----
echo $tm $pval
echo . . .
fi
ltm=tm lpval=pval
done <$in_file
Last edited by DGPickett; 08-01-2011 at 11:13 AM..
These 2 Users Gave Thanks to DGPickett For This Post:
Hi,
I need to match for the pattern '.py' in my file and print the word which contains.
For example:
cat testfile
a b 3 4.py 5 6
a b.py c.py 4 5 6 7 8
1.py 2.py 3 4 5 6
Expected output:
4.py
b.py c.py
1.py 2.py (3 Replies)
I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt
Page 2015-01-01 2000
Colors 2015-02-01 3000
Landing 2015-03-02 4000
#!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Hi all!
Thanks for taking the time to view this!
I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern.
Example:
Drink a soda
Eat a banana
Eat multiple bananas
Drink an apple juice
Eat an apple
Eat multiple apples
I... (8 Replies)
The sample file:
dept1: user1,user2,user3
dept2: user4,user5,user6
dept3: user7,user8,user9
I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
'Hi
I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match.
Which option is to be used to exclude the line containing the pattern?
sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Hi,
I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts.
/tmp/1
IP=123.456.789.01
WAS_HOSTNAME=abcdefgh.was.tb.dsdc
/tmp/hosts
123.456.789.01
I want this result in /tmp/hosts if hostname is already there dont want duplicate entry.
... (5 Replies)
Hi All,
I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/
The sample I wrote out is very basic compared to what I am actually working with but it will get me started
I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Hi,
My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present.
Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Hi all,
I'm looking for some help. I have a file (very long) that is organized like below:
>Cluster 0
0 283nt, >01_FRYJ6ZM12HMXZS... at +/99%
1 279nt, >01_FRYJ6ZM12HN12A... at +/99%
2 281nt, >01_FRYJ6ZM12HM4TS... at +/99%
3 283nt, >01_FRYJ6ZM12HM946... at +/99%
4 279nt,... (4 Replies)
Hi,
I have file 1.txt with following entries as shown:
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433
**
**
**
In file 2.txt I have the following entries as shown:
0152364|134444|10.20.30.40|015236433
0233654|122555|10.20.30.50|023365433... (4 Replies)