Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2004
    Posts
    5

    Unanswered: 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.

  2. #2
    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
    Last edited by fla5do; 01-28-04 at 16:11.
    Greetings from germany
    Peter F.

  3. #3
    Join Date
    Jan 2004
    Posts
    5
    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.

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

  5. #5
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  6. #6
    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,

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •