LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-04-2010, 04:17 AM   #1
linux_man
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Rep: Reputation: 0
Substring of string in tag + shell script


Hi,

Please help me solve the following as I am new to scripting

I have a string variable

var1=<default>111</default>

I want the value between default tags 111 in this case.

I treid to use ${var1:10:3}
But the execution gived "bad substitution' error.

If the tags are removed and substring is fetched it works fine.
Please help me.
Is there any other was to get substring in a string with tags.
Thanks i advance
 
Old 02-04-2010, 04:42 AM   #2
AngTheo789
Member
 
Registered: Sep 2009
Posts: 110

Rep: Reputation: 24
Indexing is zerobased, so it should be ${var:9:3}. Otherwise it works on my bash shell.
 
Old 02-04-2010, 04:54 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
You need quotes to assign the string:
Code:
c:~$ var1=<default>111</default>
bash: syntax error near unexpected token `111'
Now with quotes and showing a way to parse the value from between the tags
Code:
c:~$ var1='<default>111</default>'
c:~$ var1="${var1#<default>}"
c:~$ var1="${var1%</default>}"
c:~$ echo $var1
111
 
Old 02-04-2010, 05:58 AM   #4
AngTheo789
Member
 
Registered: Sep 2009
Posts: 110

Rep: Reputation: 24
Quotes are needed indeed!
 
Old 02-04-2010, 09:04 PM   #5
linux_man
LQ Newbie
 
Registered: Feb 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for ur reply...

Can you please tell me a way to extract 111 from <default>111</default> using SED command.
I do not want to hard code the character position to get the value.
 
Old 02-04-2010, 10:32 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by linux_man View Post
I do not want to hard code the character position to get the value.
The technique shown above using bash parameter expansion does not hard code the character position and runs faster than using sed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need assistance with shell script - replace a string with a start tag and end tag SupermanInNY Programming 18 01-02-2010 05:44 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Shell script: Delete filenames containing a substring? Drack Linux - General 4 02-12-2006 03:11 PM


All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration