Substring problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substring problem
# 1  
Old 09-26-2008
Substring problem

I have a file which comes with a few tags and some contents inside them.
eg:
<ABC>Hello Unix.com</ABC>
<XYZ>WWSHF</XYZ>
Now I want to pick out the contents in between the tags <ABC> and </ABC>.
The contents within <ABC> and </ABC> spans more than one line. So greping and cutting is not an option i guess.
Thanks in advance.
# 2  
Old 09-26-2008
# remove most HTML tags (accommodates multiple-line tags)
sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
Code:
$ cat tst2
<ABC>Hello Unix.com</ABC>
$ cat tst2|sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
Hello Unix.com

# 3  
Old 09-26-2008
This should work:
Code:
echo '<ABC>Hello
Unix.com</ABC>
<XYZ>WWSHF
</XYZ>' | sed 's/<[^>]*>//g'

# 4  
Old 09-30-2008
i guess i also found out a sloution from somewhere, although i'm still to come to terms with this sed command (just one of those things you know Smilie ) ...
sed -e 's/<ABC>//' -e 's/<\/ABC>//' filename > newfilename
Thanks a lot for your help.
# 5  
Old 09-30-2008
if you have PHP
Code:
<?php
$file="file";
$data=file_get_contents($file);
$data=strip_tags($data,"<ABC>");
$startindexabc = strpos($data,"<ABC>") + strlen("<ABC>");
$endindexabc = strpos($data,"</ABC>");
echo substr($data,$startindexabc,$endindexabc-$startindexabc );
?>

output
Code:
# more file
<ABC>Hello Unix.com
some other stuff
some other tags
</ABC>
<XYZ>WWSHF</XYZ>
# php5 test.php
Hello Unix.com
some other stuff
some other tags

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substring Problem with sed . . .

Greetings. I'm looking to isolate the first occurrence of an arbitrary substring which may be present at any particular line in a given file. The enclosing end markers for the target in our thought problem are string" and ". The complete string and surrounding text could look something like... (3 Replies)
Discussion started by: LinQ
3 Replies

2. Shell Programming and Scripting

Substring

Hi All, In ksh, am trying to get a substring stuff done. Not sure where the problem is.. can you guys guide me on this... for instance, var1=41, and var2=4175894567, then i want to know whether var2 starts with var1.. var1 and var2 can be of any length.. VAR1=41 VAR2=419068567777... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

3. Shell Programming and Scripting

KSH: A PROBLEM awk SUBSTRING

Hi Gurus, I am working with a korn shell script to simplify some operations of creation file of spool for a Bulk-Copy. Thi is my code: In a for cycle: gzcat ${file} |awk '{ print substr($0,1,5)";"substr($0,108,122)";"substr($0,124,131)";"substr($0,139,152)";"substr($0,154,163)";"}' >>... (6 Replies)
Discussion started by: GERMANICO
6 Replies

4. Shell Programming and Scripting

Get the substring

Hi All, I have a ouput string likes 'u8wos' or 'u10acsd' or somthing else 'u{number}{any characters}'and I want to get the number behind the letter 'u' by bash shell. Thanks Damon (11 Replies)
Discussion started by: Damon_Qu
11 Replies

5. UNIX for Dummies Questions & Answers

Need help with substring

I need to check the occurrence of one string within another. code ******************** if ;then do something done ******************** Thanks (7 Replies)
Discussion started by: w020637
7 Replies

6. Shell Programming and Scripting

Problem with Substring

Hi, i have a file from which i extracted 2nd column and put it into a variable Acct_Num . Now i need to extract first 3 digits from it and save in to other variable. My Code is: while read Record1 do Acct_Num=`echo $Record1 | awk '{print $2 }'` ID=`echo $Record1 | awk '{print... (3 Replies)
Discussion started by: manmeet
3 Replies

7. Shell Programming and Scripting

Substring HELP!

Hi, I am trying to do something which I thought was very simple but still being a beginner, has proved not to be. Input: val1 val2 val3 val4 val5 val6 . . . etc Desired Output: Every row in which value of val6 is a number starting with 0.0 or contains a capital E. The input... (2 Replies)
Discussion started by: awknerd
2 Replies

8. Shell Programming and Scripting

substring

Dear All, i have a file that contains, FROM_DATE: 06-08-2007 00:00:00 TO_DATE: 06-08-2007 23:59:59 Total number of lines: 6874154 in another file,the contain is, FROM_DATE: 06-08-2007 00:00:00 Total number of lines: 874154 alltime i want to find the particular string... (4 Replies)
Discussion started by: panknil
4 Replies

9. UNIX for Dummies Questions & Answers

problem extracting substring in korn shell

hi all, I have read similiar topics in this board, but i didn' t find the posting which is the same with the problem i face.. I try to extract string from the end. i try to do this: num=abcdefghij num2=${num:-5} echo $num2 #this should print the last 5 characters (fghij) but it doesn;t... (3 Replies)
Discussion started by: nashrul
3 Replies

10. Shell Programming and Scripting

How do I Substring ??

Hello everyone. I'm writing a script in UNIX. The purpose is to get the second character from a variable that stores the system year. This is the code: unix_year_yy=`date "+%g"` This will return "07" in variable unix_year_yy. How can I get the second character (7)?? (6 Replies)
Discussion started by: Rigger
6 Replies
Login or Register to Ask a Question