Parsing String, Search then display rows


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Parsing String, Search then display rows
# 8  
Old 04-27-2008
Thanks ripat and era.

ripat, your script will work fine but the problem is sometimes
I get only open or close within the begin and end script.

era,
Your script works beautifully. I just need one request if you dont mind.
begin and end is something I will add later. What if I dont want to add "end" and only "begin". Now I have to use the "begin" and the "begin" of the next section as an end.

Code:
begin
  open
    ......
  open
    ......
  close
    ......
begin
   .....
begin
   .....

When count=0 and begin is the next begin then display.
I want to check for the exact "begin" so
it will only look for "begin" and not "beginning" or "prebegin",
just like what you said.

Something like this but it does not work yet.

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

era, I really appreciate your help. I wish you all the best!

Last edited by buddyme; 04-27-2008 at 05:33 AM..
# 9  
Old 04-27-2008
Double quotes have no significance in regular expressions, you are looking for literal double quotes around the begin, which obviously won't be there.

However, you can check for equality: if field $1 is exactly equal to "begin" then it's a match. And if you have some data you have remembered from a previous section, print that and flush the state variables before starting over.

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

The last line copes with the case when at end of file there is no new "begin" line but you have something you want to print from the previous "begin" section.
# 10  
Old 04-27-2008
Quote:
Originally Posted by buddyme
Thanks ripat and era.

ripat, your script will work fine but the problem is sometimes
I get only open or close within the begin and end script.
Just add a condition that awk needs to see two occurrences of "open" before it parses the record.

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

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

/open.+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