Find the answer to your Linux question:
Results 1 to 6 of 6
i am a newbie in linux script. i try to manipulate the param input name and get the expected output. Here is example; #! /bin/ksh # $1 -< proj_request_tmp_table param1=$1 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    24

    shell script-remove certain characters


    i am a newbie in linux script. i try to manipulate the param input name and get the expected output.
    Here is example;
    #! /bin/ksh
    # $1 -< proj_request_tmp_table
    param1=$1
    #using cut or sed remove _tmp
    export param1
    echo "output: $param1 print out"
    #--end of script

    after remove _tmp here is my
    expected output:output: proj_request_table print out

    thanks in advance

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    How about:

    Code:
    $ cat untemp
    for file in "$@"
    do
      newfile=$(echo "$file" | sed 's/_tmp//')
      echo $newfile
    done
    $ untemp proj_request_tmp_table
    proj_request_table
    $
    This handles multiple file names so I guess it's fancier than you might need. If a file name doesn't contain "_tmp", it should just return the original name.

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    24
    Thank you so much!!!!!

  4. #4
    Just Joined!
    Join Date
    Oct 2012
    Posts
    24
    I applied your code but output looks like that "output: afaweiraw printout"
    would you mind take a look my code? i need help
    Code:
    #! /bin/ksh
    # $1 -< proj_request_tmp_table
    #param1=$1
    param1='proj_request_tmp_table'
    export param1
    newfile =$(echo "$param1" | sed 's/_tmp//')
    export newfile
    
    echo "output: $newfile print out"
    #--end of script
    Quote Originally Posted by mrbruno View Post
    How about:

    Code:
    $ cat untemp
    for file in "$@"
    do
      newfile=$(echo "$file" | sed 's/_tmp//')
      echo $newfile
    done
    $ untemp proj_request_tmp_table
    proj_request_table
    $
    This handles multiple file names so I guess it's fancier than you might need. If a file name doesn't contain "_tmp", it should just return the original name.

  5. #5
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    The biggest problem I see in your script is the space between newfile and the assignment operator in the statement calling sed. When I took it out, I think I got the behavior you expected. You can't have a space like that on either side of the assignment operator. If the value you're assigning to the variable has leading spaces, you're going to have to express the spaces (and perhaps the entire value) inside quotes or something.

    Note that the exports are unnecessary. They don't hurt but they don't help in this case. Since you're just using the variables locally in the script, there's no need to export them. You only need to export if you're invoking an external script or command and you want it to run with that variable as an environment variable.

  6. #6
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187
    are you just trying to remove tmp from the name?

Posting Permissions

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