Find the answer to your Linux question:
Results 1 to 5 of 5
Say I have this string stored in a variable var='f:\dd\vctools\crt_bld\SELF_X86\crt\src\INTEL\ mt_lib\chandler4.obj' I want to only get 'chandler4.obj' this string and store it into another variable. To be more specific, the ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2

    how to extract a string from a variable?

    Say I have this string stored in a variable

    var='f:\dd\vctools\crt_bld\SELF_X86\crt\src\INTEL\ mt_lib\chandler4.obj'

    I want to only get 'chandler4.obj' this string and store it into another variable. To be more specific, the string that comes after the \ character. I'm reading about a thousand of those from a file so it'd be nice to have a way to do the task in an automated fashion. How would one go about making a bash script that does that?
    Thanks!

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Hello,

    look into the "basename" command.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2
    Thanks! I'll look into it.

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If you find that the shell and basename do not seem to do the right thing with backslash in your Windows pathname as opposed to (forward)-slash, then re-post with your command / script ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    basename won't work with backslashes. This will work:

    Code:
    #!/bin/bash -vx
    
    var='f:\dd\vctools\crt_bld\SELF_X86\crt\src\INTEL\ mt_lib\chandler4.obj'
    echo "${var##*\\}"

Posting Permissions

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