How to fetch a string between two different strings?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fetch a string between two different strings?
# 1  
Old 04-25-2012
How to fetch a string between two different strings?

Hi

I am having a file with content as,
Code:
<name>usrname</name> <value>ravistej</value>
<name>pswd</name> <value>asdfgzxcv</value>
.
.
.
.
<name>abc</name> <value>xyz</value>

From this file,
i want to fetch the string present between '<value>' and '</value>' and store the values into another file.Smilie

Last edited by Franklin52; 04-25-2012 at 09:21 AM.. Reason: Please use code tags
# 2  
Old 04-25-2012
Try:
Code:
sed 's!.*<value>\(.*\)</value>.*!\1!' file

# 3  
Old 04-25-2012
or
Code:
# sed 's|.*value>\(.*\)<.*|\1|' infile

# 4  
Old 04-25-2012
thank you very much
it's working fine... Smilie
if you dont mine can you explain the flow of the command... Smilie
# 5  
Old 04-25-2012
Code:
nawk -F"[><]" '{for(i=1;i<=NF-1;i++){if($i~/value/){print $(i+1)}}}' input.txt

Try with awk, if you dont have nawk
# 6  
Old 04-26-2012
How to fetch a string between two different strings

Hi Raj,

Just want to know what is the meaning of -F"[<>]". I understand that we are defining delimeter but what exactly menaing of above.

Thanks
Krsnadasa
# 7  
Old 04-26-2012
Code:
awk '/^value/{print $2}' RS=\< FS=\> infile > outfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To Fetch values from String

Dears, I have string like below in my csv file <someTechnicalDetails><someTechnicalDetails>HSI</someTechnicalDetails,1234564 <someTechnicalDetails><someTechnicalDetails>Voice</someTechnicalDeta,12345673 <someTechnicalDetails><someTechnicalDetails>IPTV</someTechnicalDetai,12345673 and I... (2 Replies)
Discussion started by: mirwasim
2 Replies

2. Shell Programming and Scripting

Fetch Valid String

Hi All, How to fetch the particular row based on my search command. For example Source Column Column A Column B 001,56123456 002,57123456 003,123456 Expected Output: 003,123456 To get this output I tried below mentioned function grep '123456' filename.txt (4 Replies)
Discussion started by: suresh_target
4 Replies

3. Shell Programming and Scripting

Cut & Fetch word from string

I have a file with some SQL query, I want to fetch only Table Name from that file line by line. INPUT FILE SELECT * FROM $SCHM.TABLENAME1; ALTER TABLE $SCHM.TABLENAME1 ADD DateOfBirth date; INSERT INTO $SCHM.TABLENAME1 (CustomerName, Country) SELECT SupplierName, Country FROM $SCHM.TABLENAME2... (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

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

5. Solaris

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... (15 Replies)
Discussion started by: balajikalai
15 Replies

6. Shell Programming and Scripting

Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi I am trying to fetch the rows with match string "0000001234" Input file looks like below: 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1... (6 Replies)
Discussion started by: nareshk
6 Replies

7. Shell Programming and Scripting

To replace string between two strings

how do i replace a string within two strings ?. i have a string called my_string i.e my_string="GACAHX04GAC010000000001DDELTA 0001DAT00001320SLTZ" i need to replace all characters between DAT and SLTZ with zeros, the number of characters between these strings might vary . i.e output ... (1 Reply)
Discussion started by: amit1_x
1 Replies

8. Shell Programming and Scripting

How to fetch variable value in if block and to compare it with certain string

Hi, I am trying to execute this command if ; then but getting error .Some problem with reteriving the value of $exception_info. Please help.Its urgent. thanks (4 Replies)
Discussion started by: khushboo
4 Replies

9. Shell Programming and Scripting

fetch string and insert later

"are you billy_smith ?" replace with "are you billy_smith ? my name is billy_smith" how to fetch the name "billy_smith" and using it later I need sed script to do this, any one can help? Thanks (6 Replies)
Discussion started by: playinmel.com
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