find string nth occurrence in file and print line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find string nth occurrence in file and print line number
# 1  
Old 10-06-2011
find string nth occurrence in file and print line number

Hi

I have requirement to find nth occurrence in a file and capture data from with in lines (between lines)

Data in File.
Code:
<QUOTE>
<SESSION>
<ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/>
<ATTRIBUTE NAME='Service Name' VALUE='None'/>
</SESSION>
<SESSION>
<ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/>
<ATTRIBUTE NAME='Service Name' VALUE='None'/>
</SESSION>
</QUOTE>

I want to get the first occurrence of the <SESSION> and </SESSION> and process between lines.
Again capture data from 2nd occurrence of the <SESSION> and </SESSION> until last occurrence repeat the same.

Thanks for help.
Mallik.

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 10-06-2011 at 03:55 PM..
# 2  
Old 10-06-2011
Don't quite know what you mean by "process between lines" but this prints the lines:

Code:
sed -n '/<SESSION>/,/<\/SESSION>/p' infile

# 3  
Old 10-06-2011
Thanks for your prompt response.

Is there any way to to split the output occurrence wise. I want to print first occurrence into file1 and 2nd occurrence into file2.
# 4  
Old 10-06-2011
How about this, (produces 1.out, 2.out etc.)

Code:
sed -n '/<SESSION>/,/<\/SESSION>/p' infile | awk ' NF { print $0 RS > ++j ".out" } ' RS='</SESSION>'

# 5  
Old 10-07-2011
This is really helpful.. But this is creating n+1 output files. If i have 2 occurrences of the </SESSION> string in a file but output files are 3.

---------- Post updated at 01:49 PM ---------- Previous update was at 01:23 PM ----------

Thanks for all your help Chubler_XL.

Can you help me to find solution to below requirement.

Now i want to get the data starts with <SESSION and containing SESSIONNAME='mappingname1' , mappingname1 value is dynamic.

Example of the SESSION value

Code:
<SESSION DESCRIPTION ="" ISVALID ="YES" NAME ="s_m_FF_OM_RA_TERMS_TL_REV"  SORTORDER ="Binary" VERSIONNUMBER ="1">


Code:
sed -n '/<SESSION>/,/<\/SESSION>/p' infile | awk ' NF { print $0 RS > ++j ".out" } ' RS='</SESSION>'

---------- Post updated 10-07-11 at 01:20 AM ---------- Previous update was 10-06-11 at 01:49 PM ----------

can anyone help me to get solution for above requirement.

Appreciate all your help..

Last edited by radoulov; 10-06-2011 at 03:55 PM..
# 6  
Old 10-09-2011
Code:
sed -n '/<SESSION/,/<\/SESSION>/p' infile | awk ' NF { print $0 RS > ++j ".out" } ' RS='</SESSION>'

# 7  
Old 10-10-2011
Thanks for solution.. But i was looking for.

line starts with '<SESSION ' and also contains 'NAME=' in same line anywhere as a first parameter. this will gives me exact session which i want and that combination is always unique.

I want to try some thing like below.
Code:
sed -n '/ <line starts with <SESSION and contains 'NAME=$sessionname'> /,/<\/SESSION>/p' infile | awk ' NF { print $0 RS > ++j ".out" } ' RS='</SESSION>'

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-10-2011 at 03:22 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

4. Shell Programming and Scripting

Print nth line in a file

Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Can I use grep in this case ? Example: In the below file, 12th line is "Kernel parameter check passed for rmem_max" . I just want the 12 line to be printed. # cat sometext.txt Kernel version check... (2 Replies)
Discussion started by: John K
2 Replies

5. Shell Programming and Scripting

Find a string occurrence if twice in a line

Hello All, I want to check if a delimiter is existing twice in a line of a text file. Suppose flat file is like this 234 | 123 123 | 345 456 | 563 | 234 | 548 So the the 3rd line has two delimiters, How can we find the lines in such a file having more then one delimiters I tried... (5 Replies)
Discussion started by: nnani
5 Replies

6. Shell Programming and Scripting

Count number of occurrence of a string in file

if there's a file containing: money king money queen money cat money also money king all those strings are on one line in the file. how can i find out how many times "money king" shows up in the line? egrep -c "money king" wont work. (7 Replies)
Discussion started by: SkySmart
7 Replies

7. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

8. Shell Programming and Scripting

how to find third(nth) word in all line from a file

For example i'm having the below contents in a file: expr is great when you want to split a string into just two parts. The .* also makes expr good for skipping a variable number of words when you don't know how many words a string will have. But expr is lousy for getting, say, the fourth word... (2 Replies)
Discussion started by: bangarukannan
2 Replies

9. Programming

Find and print number after string in C

I'm trying find and print a number after a specific user passed string in each line of a text file using C (as requested by the powers that be). I've pieced together enough to read the file, find the string and print the line it was found on but I’m not sure where to even start in terms of finding... (3 Replies)
Discussion started by: cgol
3 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question