Find the answer to your Linux question:
Results 1 to 6 of 6
Hello, I have a situation where I need to remove the text "radio28" including the quotes from many shell scripts with the .sh extension. I have been learning SED and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    New York City - USA
    Posts
    21

    Sed Assistance


    Hello,

    I have a situation where I need to remove the text "radio28" including the quotes from many shell scripts with the .sh extension. I have been learning SED and AWK from this site and other references. My goal is to automate this in a bash shell script on RHEL4.

    The files are located in /usr/local/bin

    Currently my attempt at this worked. However I used redirection to a tmp folder and I lost all my file attributes. I know there is a way to use the -i flag. However I have not been successful.

    Here is what I have so far:

    Code:
    #! /bin/bash
    
    for x in `ls /usr/local/bin/` ; do
    
    
     sed 's/"radio28"/ /g' /usr/local/bin/$x > /usr/local/bin/tmp/$x
    
    done
    Is there a way to run this from inside of /usr/local/bin without wrecking the attributes. Also without the redirection.

    Your help is appreciated.

    Jaysunn

  2. #2
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    What do you mean not successful with the -i option? What happened?

    The command should look like:

    Code:
    sed -i 's/"radio28"/ /g' /usr/local/bin/$x

  3. #3
    Just Joined! jaysunn's Avatar
    Join Date
    Apr 2009
    Location
    New York City - USA
    Posts
    21
    When adding that line to my script:
    Code:
    #! /bin/bash
    
    for x in `ls /usr/local/bin/` ; do
    
    
    sed -i 's/"radio28"/ /g' /usr/local/bin/$x
    
    done
    When executed I receive this Message. "This may be related to me testing on MAC OSX Terminal". I get the below error for every file sed tries to input.

    Code:
    sed: 1: "/usr/local/bin/check_gr ...": extra characters at the end of l command


    Thanks for the reply.

    Jaysunn

  4. #4
    Linux Newbie
    Join Date
    Mar 2009
    Posts
    228
    If you're testing on OSX and not Linux then that's the problem. The -i option in OSX sed requires a value. You have to test this on Linux.

    Here's a link to OSX sed manpage.
    Mac OS X Manual Page For sed(1)

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by jaysunn View Post

    Code:
    #! /bin/bash
    
    for x in `ls /usr/local/bin/` ; do
    
    
     sed 's/"radio28"/ /g' /usr/local/bin/$x > /usr/local/bin/tmp/$x
    
    done
    use shell expansion
    Code:
    for x in /usr/local/bin/*

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by jaysunn View Post
    I need to remove the text "radio28" including the quotes from many shell scripts with the .sh extension.
    The files are located in /usr/local/bin
    [/CODE]

    Is there a way to run this from inside of /usr/local/bin without wrecking the attributes. Also without the redirection.

    Jaysunn
    basically, you need to remove "radio28" from shell scripts that have an .sh extension
    and are located in /usr/local/bin and are executable
    Right?
    in that case I would do this

    cd /usr/local/bin
    for f in *.sh
    do
    if [ $(grep -c '"radio28"') -gt 0 ]; then
    sed 's/"radio28"//g' $f > __f && mv __f $f && chmod 755 $f
    fi
    done

    note:
    the grep is for doing the job only for the files that actually have that string
    chmod 755 should make the script executable
    and this is done in place (no need to use /tmp)
    of course you must have the right permissions to write in /usr/local/bin

Posting Permissions

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