Get occurence of "open" considering duplicates(get the last open).
Once you are pointing to the last open count 2 rows to get the correct data.
Every begin and end statement, there is a "close" and "open".
There can be many "close" and "open" within the begin and end statement but
we are concerned only on the last entry of "open".
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Input file:
begin
open
datacall1
data1
close
datacall1
data2
open
datacall1
data3
end
begin
close
datacall1
data4
open
datacall1
data5
close
datacall1
data6
end
begin
....
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Your description is not very understandable, but if I understand correctly, you want the data after the last open before each end?
When we see an open, we remember it. When we see a data and have seen an open, we remember the data line, and reset the open state variable to 0. So if more open lines come after this one, but before the end, they will replace the data we have in memory. Now simply print that data when you see an end, and reset the state variables (open, too, just in case).
Thank you so much era.
It is very close, sorry I know my example is so confusing.
Let me give another example. Basically when you see the last "open" you count 2 rows to get that line.
Below, when it sees the "open", it counts 2 rows to get "visualbasic".
As an added reqt, I want the date too, "20080204|visualbasic".
----------------------------------------------------------------------
Output:
20080204|visualbasic
20080209|<nextdata>
If the records are separated by an empty line as in your sample file above, and if there is *exactly* the same number of fields in a record, it's easy:
Will not work if the conditions above are not met.
Here's a slight revamp of my earlier script. When you see "open", start a countdown, and when that reaches zero, grab that line and remember it in data. Grabbing the date (I called it "heading"; maybe you want to change that) is a trivial addition. I set the internal field separator to '|' to make it easy to get the date.
This doesn't trim whitespace before the data value. If that's required, or if the -F option causes trouble elsewhere in the file, maybe something like
If the indentation patterns are consistent throughout the file, maybe you want to tighten up the regular expressions to /^begin/ (flush at start of line), /^ open/ (one space before "open"), and /^end/ (flush at start of line, again) in order to avoid accidental matches (maybe you have "openoffice" or "blender" somewhere in those data?)
(Hmm, ripat's use of split to extract the date from the heading is certainly more elegant than my attempt. I'll leave it just to show that there is always more than one way to do it.)
Last edited by era; 04-27-2008 at 03:16 AM..
Reason: split() would have been more elegant, harumph
And if the record begin --> end has a variable number of fields, there is also a solution using open as field separator. This will isolate the second group of open. A little string manipulation should return the desired result. But you still need an empty line as record separator.
Experts,
Need your support for this awk script.
we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file
Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
I have 3 files. Each of those files have the same number of records, however certain records have different values. I would like to grep the field in ALL 3 files and display the output with only the differences in column wise and if possible line number
File1
Name = Joe
Age = 33... (3 Replies)
I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file.
Please use CODE tags as... (7 Replies)
Hi,
Need your help for this scripting issue I have. I am not really good at this, so seeking your help.
I have a file looking similar to this:
Hello, i am human and name=ABCD.
How are you?
Hello, i am human and name=PQRS.
I am good.
Hello, i am human and name=ABCD.
Good bye.
Hello, i... (12 Replies)
I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios.
Cat test
Nov 10, 2012 5:17:53 AM
INFO: Request Type
Line 1.... (5 Replies)
I'm getting the input row in this format it contains 2 alphabets followed by numbers between 1 and 7 and again 2 alphabets followed by numbers between 1 and 7.
Now I need to parse this input into several output rows in this format 2 alphabets followed by each number occurrence
suppose... (5 Replies)
Hello Gurus,
Apologies if this Q has been repeated but i was not able to find it :(
I have an input file:
-------------------------------
Replace DB.Employee
as
select
column1
column2
from DB_T.Emp
and DB.Test
and
DB.Dept
and
DB_T.Ter;
------------------------ (4 Replies)
Hi,
I have a file that looks like this (tab seperated):
Barry -3 -4 -5 -10 -4 6 -8 20 -6 NaN NaN NaN
Brend -2 4 -3 -7 -3 8 -9 -10 -6 NaN NaN NaN NaN NaN
Harry -10 -9 -40 6 -7 3 -7 -2 -5 NaN NaN NaN NaN NaN NaN NaN
I want to print the first column... (4 Replies)
Hey guys,
I have this file generated by me... i want to create some HTML output from it.
The problem is that i am really confused about how do I go about reading the file.
The file is in the following format:
TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
In my script I read a input file and search all the files in a directory and it's sub-directories for that string using:
find . -type f -print | xargs grep $var1
This just displays all the lines the string was found on. Too much data.
What I need is to store in a file one time those... (17 Replies)