Sponsored Content
Top Forums Shell Programming and Scripting File Reading for a certain string and echo the line before Post 302605878 by MKNENI on Thursday 8th of March 2012 11:29:12 PM
Old 03-09-2012
File Reading for a certain string and echo the line before

Hi Guys,

I have a big file like this. It has cache group line ( the bold lines ) and then followed by 10 status lines showing either Compelte or Failed. This pattern repeats several time. We can assume all the status lines have either Complete or Failed status for that Cache Group line.

I need to find a way to display just the failed cache group lines ( echo of that line ). Here I just want to see one line "call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_PLAN');"

Have been struggling for a day but not finding a good solution. Can something like this be achieved using shell scripting or do I have to use perl ?

Hope I didn't confuse in explaining what I want.

Code:
call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_NOTE');
< 2284744, 2012-03-08 22:57:11.000000, 24829530, 11422, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:57:09.000000, 24827530, 11421, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:57:07.000000, 24825530, 11420, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:57:05.000000, 24823530, 11419, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:57:03.000000, 24821520, 11418, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:57:01.000000, 24819520, 11417, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:56:59.000000, 24817520, 11416, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:56:57.000000, 24815520, 11415, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:56:55.000000, 24813520, 11414, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
< 2284744, 2012-03-08 22:56:53.000000, 24811520, 11413, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Complete >
10 rows found.

call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_PLAN');
< 2285336, 2012-03-08 22:57:13.000000, 24830610, 2483, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:57:03.000000, 24820610, 2482, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:53.000000, 24810610, 2481, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:43.000000, 24800590, 2480, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:33.000000, 24790580, 2479, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:23.000000, 24780580, 2478, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:13.000000, 24770580, 2477, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:56:03.000000, 24760580, 2476, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:55:53.000000, 24750580, 2475, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
< 2285336, 2012-03-08 22:55:43.000000, 24740580, 2474, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Failed >
10 rows found.

Thanks in Advance
mkneni

Last edited by Scott; 03-10-2012 at 06:55 AM.. Reason: Code tags, please
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to echo the file contents LINE BY LINE

hello, i have a listing (let say ABC) consists of the below: : public database link public synonym role rollback segment : when i run the below for loop, for i in `more ABC` do echo "$i" done it gives me, : public database (4 Replies)
Discussion started by: newbie168
4 Replies

2. UNIX for Dummies Questions & Answers

Reading one line from a multiline string

Hi , I have a string which is of multiple line,I have to split the string by reading one line in each iteration and keep it in a string.I am keeping all the file names in side as string returned by the ls command ,I am stocked at the point of spliting the string to segragate each file. ... (4 Replies)
Discussion started by: Deekay.p
4 Replies

3. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

4. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

5. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

6. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

7. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

8. Shell Programming and Scripting

Echo multi-line string via heredoc syntax

$ cat bashtest #!/usr/local/bin/bash echo <<<"EOF" line1 line2 line3 EOF $ ./bashtest ./bashtest: line 3: line1: command not found ./bashtest: line 4: line2: command not found ./bashtest: line 5: line3: command not found ./bashtest: line 6: EOF: command not found What am i doing... (4 Replies)
Discussion started by: urello
4 Replies

9. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies
Regexp::List(3pm)					User Contributed Perl Documentation					 Regexp::List(3pm)

NAME
Regexp::List - builds regular expressions out of a list of words SYNOPSIS
use Regexp::List; my $l = Regexp::List->new; my $re = $l->list2re(qw/foobar fooxar foozap fooza/); # $re is now qr/foo(?:[bx]ar|zap?)/ ABSTRACT
This module offers "list2re" method that turns a list of words into an optimized regular expression which matches all words therein. The optimized regular expression is much more efficient than a simple-minded '|'-concatenation thereof. DESCRIPTION
This module use Object-Oriented approach so you can use this module as a base and tweak its features. This module is a base class of Regexp::Optimizer. EXPORT Since this is an OO module there is no symbol exported. METHODS
This module offers methods below; $l = Regexp::List->new(key=>value, ...) Constructor. When arguments are fed in key => value, manner, it sets attributes. See "$l->set" for details $re = $l->list2re(list of words ...) Does the job. Takes a list of words and turn it into an optimal regular expresson. See "IMPLEMENTATION" to find out how it is achieved. If you want to know the underlying black magic even further, see the source. $l->set(key => value, ...) Sets attributes. There are many attributes supported but let me mention just a few that you may be interested. lookahead Whether you prepend a lookahead assertion or not. Default value is 1. This module is smart enough to omit the assertion when you don't need one. $re = $l->list2re(qw/1 2 3 infinity/); # qr/(?=[123i])(?:[123]|infinity)/ $re = $l->set(lookahead=>0)->list2re(qw/1 2 3 infinity/); # qr/(?:[123]|infinity)/ quotemeta Whether you quote metacharacters or not. Default is 1. If you really need this feature try Regexp::Optimizer instead. @list = qw/3 3.14 3.14159265358979/; $re = $l->list2re(@list); # qr/3(?:.14(?:159265358979)?)?)/ $re = $l->set(lookahead=>0)->list2re(@list); # qr/3(?:.14(?:159265358979)?)?)/ # which does match 3.14 but also "11+3=14" modifies Currently it accepts 'i', 'm', 's', and 'x', the same as regular expression modifiers. @list = qw/Perl perl BASIC basic/; $re = $l->list2re(@list); # qr/(?=[BPbp])(?:[Pp]erl|BASIC|basic)/ $re = $l->set(modifiers => 'i')->list2re(@list); # qr/(?=[bp])(?:perl|basic)/i print $l->set(modifiers => 'x')->list2re(@list); # Try for yourself; Isn't itcute ? $l->expand($re); Utility methods to expand a regular expression. Handy when you want to check the complex regexes. $l->unexpand($re); Utility methods to unexpand a regular expression. IMPLEMENTATION
This module optimizes the regular expression as follows. Let's see what happens when qw/foobar fooxar foozap fooza/ is fed trie building (common prefix aggregation) first the corresponding trie structure is built +- bar foo -+- xar +- za -+- p +- '' common suffix aggregation it checks if any leaf node can be optimized for common suffix. In this case we can do so to "bar" and "xar". +- b -+-ar foo -+- x -+ +- za -+- p +- '' character class conversion If a branch contains more than two single characters, it turns it into a character class. foo -+- [bx] --- ar +- za -+-p +- '' empty leaf to "?" Empty leaf is converted to a '?' quantifier foo -+- [bx] --- ar +- za -+-p? join all leafs into a group And the final result is reached. foo(?:[bx]ar|zap?) BENCHMARKS
This module is faily robust. You can practically use this module to find a regular expression that matches all words in a dictionary. Here is a result by on perl 5.8.0, FreeBSD 4-Stable, Pentium III 800 Mhz with 512 MB RAM. # Sat May 31 09:11:06 2003 ( 0.000000 s) Reading /usr/share/dict/words # Sat May 31 09:11:07 2003 ( 0.847797 s) 235881 lines read. # Sat May 31 09:11:07 2003 ( 0.000000 s) Making regexp. # Sat May 31 09:13:09 2003 ( 121.596928 s) Done. # Sat May 31 09:13:09 2003 ( 0.000000 s) Saving to t/words.rx # Sat May 31 09:13:09 2003 ( 0.000000 s) Reading t/words.rx # Sat May 31 09:13:13 2003 ( 3.679176 s) Done. # Sat May 31 09:13:13 2003 ( 0.000000 s) Opening /usr/share/dict/words for comparison. # Sat May 31 09:13:13 2003 ( 0.255222 s) /usr/share/dict/words:235881 lines found. # Sat May 31 09:13:13 2003 ( 0.000000 s) Showtime! # 235881/235881 # Sat May 31 10:44:17 2003 ( 5464.370409 s) Done. # Sat May 31 10:44:17 2003 ( 5464.370624 s) 43.167 matches/s The result of optimization is obvious as the number of alteration increases. Here is a result of a benchmark which matches randomly picked words against "/usr/share/dict/words". ==== 2 words Rate naive optim naive 1.79/s -- -28% optim 2.49/s 39% -- ==== 256 words s/iter naive optim naive 31.7 -- -81% optim 5.95 433% -- SEE ALSO
Regexp::Optimizer -- uses this module as its base "eg/" directory in this package contains example scripts. Perl standard documents perltodo, perlre CPAN Modules Regexp::Presuf, Text::Trie Books Mastering Regular Expressions <http://www.oreilly.com/catalog/regex2/> AUTHOR
Dan Kogai <dankogai@dan.co.jp> COPYRIGHT AND LICENSE
Copyright 2003 by Dan Kogai, All Rights Reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-11-16 Regexp::List(3pm)
All times are GMT -4. The time now is 05:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy