Find the answer to your Linux question:
Results 1 to 5 of 5
Ok here is a problem that stumps me scripting with sed I get we can have sed search ad replace for example i have a file called test_add1.cpp and so ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Location
    Ann Arbor, MI
    Posts
    22

    using sed for changing file names

    Ok here is a problem that stumps me scripting with sed

    I get we can have sed search ad replace for example i have a file called test_add1.cpp
    and so on with several add with a number followed by the .cpp
    if i wanted to change test_ to finish i would
    Code:
     sed s/test_\(.*\)/final\1/'
    and this would change all instances.

    How about if i need to write a script that could change delete a portion of the file name by passing two arguments from the command line
    like if the script was called dropend
    Code:
    $./dropend myfile.bak .bak
    and the result would be the file called myfile and you can always ussume there will always be a period.

    im thinking something like using a
    Code:
    newfilename=$ (echo $1| sed with the back quote technique)
    and then
    doing something like
    Code:
    mv $1 $newfilename
    but i have no idea where to use $2 which is the second argument passed in to the script from the command line. I am stumped here.

    Please if some one can tell me where im going wrong?

    Thanks,
    Joe

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    I'll give you a start and let you work from it:

    Code:
    echo myfile.bak | sed "s/\.bak//"
    Cheers

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    no need external commands in bash
    Code:
    a="hello.bak"
    echo ${a%.bak}

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Location
    Ann Arbor, MI
    Posts
    22
    hey bird man thank you. I am using a script that works where you give it a file name and then the second argment giving is what you waant deleted off the file. It will not always start at the period example
    [CODE] $dropend example.exe.tar .tar [CODE]

    the script needs to pass the two arguments and make the change to the file name. so i cant hard code the files name or the part needing deleted. i need something that used $1 and $2 only im not getting how i can pass this to sed ???

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Location
    Ann Arbor, MI
    Posts
    22
    hey thanks birdman you got me thinking on the right path

    her is the new script
    #script that drops desired parts off #filenames
    newfile=`echo $1 | sed "s/$2//"
    mv $1 $newfile
    works like a charm thank you so much

Posting Permissions

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