Search string between the strings


 
Thread Tools Search this Thread
Operating Systems Solaris Search string between the strings
# 1  
Old 10-12-2010
Java Search string between the strings

File name : Sample.txt

<ownername>Oracle< ownername>

I am new to unix world , i would like to search string and return back to another sh script.

bascially i want to read file Sample.txt find the string between <ownername> Sample.txt < ownername> .
Gerneric way to find the string between <>x<>.Could you please help me to get the code for the same.

Output should be : Oracle

Best Regards,
Baaaalaaaa
# 2  
Old 10-12-2010
Code:
grep '<ownername>[^<]*Sample.txt[^<]*</ownername>'

Narrative: grep for the tag and then some characters not < and then the string and then more not < and then the closing tag.

grep can find the lines with string in tags, but sed can dig out the string. This assumes one per line:

Code:
sed '
  s/.*<ownername>\([^<]*\)<\/ownername>.*/\1/
  t
  d
 '

Narrative: select in every line with a regex that captures the whole line but a substring captures the string between tags, and replace the whole line with that substring. If the replacement occurs, branch to end of script (print and go to next line) else delete line.

I like sed because the bits and pieces are reusable in sed, vi, ex, ed, grep, egrep, ksh command line editing, C, JAVA, PERL, awk.
# 3  
Old 10-13-2010
Hi
Thank you so much for your help.Actually i have missed out few items.

Input File name : Sample.txt

line 1
<schema>database<schema>
line 2
line 3
line 4
<schema>Oracle<schema>
line 5

Actually i would like to read <schema>database<schema>string from input file and return only once database as my output.

output : database

Sorry to bother you , Please guide me for the same.
Thanks in advance

Baaalaaa

---------- Post updated at 11:17 PM ---------- Previous update was at 11:01 PM ----------

Just executed this script in unix command prompt grep '<ownername>[^<]*test.txt[^<]*</ownername>'

i didnt get any output but i press ctr c to come out;
# 4  
Old 10-13-2010
Please clarify what the input format is. In your first posting, there is a space before the ending tag label, in your last one, there is no more space. Everyone is expecting a / there but your files might not be xml. In that case, that might be:
Code:
'<ownername>[^<]*test.txt[^<]*<ownername>'

# 5  
Old 10-13-2010
Input file : test.txt

<owner_name>balaji<owner_name>


I have tired the same command in UNIX box , but i am not getting any output , control is in same place

w : 32 :/sh
%
grep '<owner_name>[^<]*test.txt[^<]*<owner_name>'
# 6  
Old 10-13-2010
Code:
grep '<owner_name>[^<]*<owner_name>'

# 7  
Old 10-13-2010
Quote:
Originally Posted by balajikalai
Hi
Thank you so much for your help.Actually i have missed out few items.

Input File name : Sample.txt

line 1
<schema>database<schema>
line 2
line 3
line 4
<schema>Oracle<schema>
line 5

Actually i would like to read <schema>database<schema>string from input file and return only once database as my output.

output : database

Sorry to bother you , Please guide me for the same.
Thanks in advance

Baaalaaa

---------- Post updated at 11:17 PM ---------- Previous update was at 11:01 PM ----------

Just executed this script in unix command prompt grep '<ownername>[^<]*test.txt[^<]*</ownername>'

i didnt get any output but i press ctr c to come out;
Well, not having given it a file or piped in a stream, it was grep'ing stdin = your keyboard.

The sed command q stops it, printing the current buffer if not -n.

Code:
sed '
  s/.*<schema>\([^<]*\)<schema>.*/\1/
  t quit
  d
  :quit 
  q
 ' Sample.txt

or use sed -n, not usually my choice as it ends up being longer:

Code:
sed -n '
  s/.*<schema>\([^<]*\)<schema>.*/\1/
  t pquit
  b
  :pquit 
  p
  q
 ' Sample.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search strings from a file in files in a directory recursively; then print the string with a status

Hi All, I hope somebody would be able to help me. I would need to search a string coming from a file, example file.txt: dog cat goat horse fish For every string, I would need to know if there are any files inside a directory(recursively) that contains the string regardless of case.... (9 Replies)
Discussion started by: kokoro
9 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. 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

4. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 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

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

7. Shell Programming and Scripting

awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles. searchstringfile.txt J./F. Gls. Wal F. Towerinput1.txt What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12 input2.txt Awk shall search for F. 16 pt. J./F. 22 output.txt input1.txt J./F. = 12 var... (3 Replies)
Discussion started by: sdf
3 Replies

8. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

9. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

10. Shell Programming and Scripting

How to concatenate two strings or several strings into one string in B-shell?

like connect "summer" and "winter" to "summerwinter"? Can anybody help me? thanks a lot. (2 Replies)
Discussion started by: fontana
2 Replies
Login or Register to Ask a Question