Find the answer to your Linux question:
Results 1 to 4 of 4
I have a variable saying $remaining having value as "/etc /var /sns /vol"(which is assigned at runtime) i want to check whether '/sns' is present in that string. If yes, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5

    Lightbulb how to remove a particular word from a string in Shell scripting


    I have a variable saying $remaining having value as "/etc /var /sns /vol"(which is assigned at runtime)

    i want to check whether '/sns' is present in that string. If yes, it should be removed and then the string must be like $remaining="/etc /var /vol"
    is there any command available to check for particular substring and remove that.
    Plz help.Thnx in advance

  2. #2
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    Code:
    remaining="/etc /var /sns /vol"
    remaining=`echo $remaining | sed "s/\/sns //g"`
    Regards

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,283
    Hi.

    Also for bash:
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate bash pattern matching in variable contents.
    
    set -o nounset
    echo
    echo "GNU bash $BASH_VERSION" >&2
    echo
    
    remaining="/etc /var /sns /vol"
    echo " variables remaining = :${remaining/??sns/}:"
    
    exit 0
    producing:
    Code:
    % ./s1
    
    GNU bash 2.05b.0(1)-release
    
     variables remaining = :/etc /var /vol:
    (with help from O''Reilly Learning the bash Shell, 2nd, pages 99 ff) ... 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 )

  4. #4
    Just Joined!
    Join Date
    Jul 2007
    Posts
    5
    thank you bird man and Drl

Posting Permissions

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