Results 1 to 8 of 8

Thread: bash scripting question

  1. #1

    bash scripting question

    i have the variable $filename which i'd like to strip of it's extension. i mean for example in perl i'd simply do
    Code:
    $filename =~ s/(.+?)\..*/$1/;
    and that'd leave me with the name minus the extension. Has anyone any ideas about how i can do this with a bash script?

    thanks,
    -Sean

  2. #2

    Re:bash scripting question

    hello,
    this will rename anything with an extention of tmp to what ever it is w/o the .tmp extention - not sure why the *.* is not taking hold...

    Code:
    #!/bin/bash
    for i in *.*
    do
    name=$(echo $i|sed 's/\.tmp$//')

  3. #3

    Re:bash scripting question

    Works for me

    Just a quick extension to the question...i only need to strip the types .ram and .rm, short of repeating the RE on the next line is there a way i can do this?
    my weak attempt didn't seem to fly...
    Code:
    name=$(echo $i|sed 's/\.["ram"|"rm"]$//')

  4. #4
    Guest

    Re:bash scripting question

    Try || or /

  5. #5

    Re:bash scripting question

    ahh, never mind. Through use of consumate .'s (well one...) i managed.
    Code:
    name=$(echo $i|sed 's/\..*$//')
    thanks for the help, pbharris
    - Sean

  6. #6

    Re:bash scripting question

    perl can be used from the command line too:

    foo=$(echo $i | perl -e 's/foo/bar/g&#039

  7. #7
    Associate
    Join Date
    Dec 2002
    Posts
    13

    Re:bash scripting question


    You can also do this without any external program, using plain bash:

    Code:
    #!/bin/bash
    
    tmp1="file1.ram"
    tmp2="file2.rm"
    
    shopt -s extglob
    
    echo ${tmp1%.@(ram|rm)}
    echo ${tmp2%.@(ram|rm)}

  8. #8

    Re:bash scripting question

    [quote author=Hko link=board=9;threadid=7681;start=0#msg70211 date=1062779536]You can also do this without any external program, using plain bash:[/quote]

    very cool, i always thought calling sed seemed unneeded for something so simple, but it was usually quick scripts i'd never use again so i never really looked. thanks for the tip!


Similar Threads

  1. Bash scripting and the stderr
    By 10Dedfish in forum Programming
    Replies: 2
    Last Post: 12-30-2003, 01:12 PM
  2. Bash scripting question
    By kenshi in forum Programming
    Replies: 6
    Last Post: 01-02-2003, 08:36 PM
  3. need some bash scripting help
    By mountainman in forum Programming
    Replies: 2
    Last Post: 08-30-2002, 12:44 PM
  4. bash scripting and quoting
    By in forum Programming
    Replies: 5
    Last Post: 03-21-2002, 12:51 AM
  5. Bash Scripting
    By cuban in forum Programming
    Replies: 3
    Last Post: 09-10-2001, 01:03 AM

Bookmarks

Posting Permissions

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