Parsing String, Search then display rows


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Parsing String, Search then display rows
# 1  
Old 04-25-2008
Search a line then display next 2 rows

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Output:
begin|data3
begin|data5
...........
begin|datax

Thank you for help. It is very much appreciated.

Last edited by buddyme; 04-26-2008 at 10:34 PM..
# 2  
Old 04-25-2008
This is what I have so far.

$ awk -F " " /open/'{getline;getline;print $0}' openclose
data1
data3
data5

The problem is, I have to compare data1 and data3 and decides data3 is the last one.

Using sed constructs but still need to get data3 and not display data1
$ sed -n '/open/{n;n;p;}' openclose
data1
data3
data5

Last edited by buddyme; 04-25-2008 at 07:10 PM..
# 3  
Old 04-26-2008
Your description is not very understandable, but if I understand correctly, you want the data after the last open before each end?

Code:
awk '/open/ { open = 1 }
open && /^data[0-9]/ { data=$0; open = 0 }
/end/ { print data; data = ""; open = 0 }' file

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).
# 4  
Old 04-26-2008
Thank you so much era. Smilie
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".
----------------------------------------------------------------------
Code:
begin|20080204
 open
   oracle   
   adabas
   server2000
 close 
   html 
   php
   java
   program1
   tcpcaller
 open
   applet
   visualbasic
   winrunner
   qtp
   loadrunner
end

begin|20080409
.....
end

Output:
20080204|visualbasic
20080209|<nextdata>

Thanks! Any help is very much appreciated.

Last edited by buddyme; 04-26-2008 at 10:31 PM..
# 5  
Old 04-27-2008
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:

Code:
#!/usr/bin/awk -f

BEGIN {
        RS=""
        FS="\n"
}

{
        split($1, out, "|")
        sub(/ +/, "", $14)
        print out[2] "|" $14
}

Will not work if the conditions above are not met.
# 6  
Old 04-27-2008
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.

Code:
awk -F '|' '/begin/ { heading = $2 }
/open/ { count = 2; next }
count { if (! --count) data = $0 }
/end/ { print heading "|" data; heading = data = ""; count = 0 }' file

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

Code:
awk '/begin/ { heading = $0; sub (/^[^|]+\|/, "", heading) }
/open/ { count = 2; next }
count { if (! --count) data = $1 }
/end/ { print heading "|" data; heading = data = ""; count = 0 }' file

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
# 7  
Old 04-27-2008
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.
Code:
#!/usr/bin/awk -f

BEGIN {
        RS=""
        FS="open"
}

{
        split($1, dte, "|")
        split($3, out, "\n")
        gsub(/\n| /, "", dte[2])
        sub(/ +/, "", out[3])
        print dte[2] "|" out[3]
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

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)
Discussion started by: as7951
6 Replies

2. Shell Programming and Scripting

Search string in multiple files and display column wise

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)
Discussion started by: sidnow
3 Replies

3. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

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)
Discussion started by: ANKIT ROY
7 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

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)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

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)
Discussion started by: laknar
5 Replies

6. Shell Programming and Scripting

Parsing the string into several rows

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)
Discussion started by: abhijith321
5 Replies

7. Shell Programming and Scripting

Search a String and display only word.

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)
Discussion started by: indrajit_u
4 Replies

8. Shell Programming and Scripting

parsing rows

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)
Discussion started by: gisele_l
4 Replies

9. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

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)
Discussion started by: umar.shaikh
8 Replies

10. Shell Programming and Scripting

Search for string and display those NOT found

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)
Discussion started by: John Rihn
17 Replies
Login or Register to Ask a Question