If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > discarding last n characters of a string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-04, 17:47
cjaykumar cjaykumar is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
discarding last n characters of a string

How do I discard the last n characters of a string.

For example,

x=abcd1234.xyz

I want

y=abcd1234

What do I need to apply on x to get y?

Also related, I want to discard the characters before 1234

so y would have

y=1234

after the manipulation.

I know I can use perl substring but I am trying to do this in shell.

Also x may be 'abcdef1234.myfile' , hence care should be taken to discard a variable number of characters both back or front without specifying the position of beginning of the substring '1234'.

thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 01-28-04, 15:05
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi,
try this :
If your string x contain a pipe then you have to change the FS variable and the line "z=$x......." to another character which surely not contains in x.

for example :

z=$x"@"$a"@"$b
y=`echo $z | awk ' BEGIN { FS="@"}


Hope, I can help you out by your problem !


x="abcdef1234.myfile"
echo "How many charakters do you want to discard in the front ?"
read a
echo "How many charakters do you want to discard at the end ?"
read b
z=$x"|"$a"|"$b
y=`echo $z | awk ' BEGIN { FS="|"}
{
y=substr($1,$2+1)
len=length(y)
y=substr(y,1,len-$3)
print y
}'`
echo "old_string "$x
echo "new_string "$y
__________________
Greetings from germany
Peter F.

Last edited by fla5do; 01-28-04 at 15:11.
Reply With Quote
  #3 (permalink)  
Old 01-28-04, 16:53
cjaykumar cjaykumar is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
Quote:
Originally posted by fla5do
Hi,
try this :
If your string x contain a pipe then you have to change the FS variable and the line "z=$x......." to another character which surely not contains in x.

for example :

z=$x"@"$a"@"$b
y=`echo $z | awk ' BEGIN { FS="@"}


Hope, I can help you out by your problem !


x="abcdef1234.myfile"
echo "How many charakters do you want to discard in the front ?"
read a
echo "How many charakters do you want to discard at the end ?"
read b
z=$x"|"$a"|"$b
y=`echo $z | awk ' BEGIN { FS="|"}
{
y=substr($1,$2+1)
len=length(y)
y=substr(y,1,len-$3)
print y
}'`
echo "old_string "$x
echo "new_string "$y





Yes, it worked! THANKS A LOT!!!

-Jay in San Francisco, Sunny California.
Reply With Quote
  #4 (permalink)  
Old 01-29-04, 11:01
asram asram is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
This should be simple.

You can try this.

x=abcd1234.xyz

To get rid off .xyz from x
y=${x%%.*}
echo $y (Should give out abcd1234)

To get rid off 1234 from y
z=${y%%1234*}
echo $z (Should see abcd)
Reply With Quote
  #5 (permalink)  
Old 01-31-04, 15:21
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Another method

x=abc1234.xyz
p=3
s=4

echo "$x" | sed -e 's/.\{3\}\(.*\).\{4\}$/\1/' => 1234

echo "$x" | sed -e 's/.\{'$p'\}\(.*\).\{'$s'\}$/\1/' => 1234
__________________
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 02-02-04, 16:09
cjaykumar cjaykumar is offline
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks!

Thank you all. I definitely have learned something from everyone here.

AS Ram, Your solution is very elegant!

-jay,
Reply With Quote
  #7 (permalink)  
Old 02-02-04, 17:00
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Sorry Mr.cjaykumar,
that was your question !

//Also x may be 'abcdef1234.myfile' , hence care should be taken to discard a variable number of characters both back or front without specifying the position of beginning of the substring '1234'.//

I dont know how you can specify any variable number of characters both back or front in the solution from asram.
__________________
Greetings from germany
Peter F.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On