Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I feel I should know this but I"m drawing a blank: How do you find a character in a string that's from a variable or from a command you've just ...
  1. #1
    Just Joined!
    Join Date
    Jun 2005
    Location
    San Francisco, CA
    Posts
    54

    how does you find a character in a string (not a file)?

    I feel I should know this but I"m drawing a blank:

    How do you find a character in a string that's from a variable or from a command you've just run (`command`)? Is there a single shell command that does this?

  2. #2
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    It depends on the context. Are you looking for a specific string or just say the third character of the string for example.

    To just check for a character you could use grep
    Code:
    grep A $(command_to_be_checked)
    grep A $SOME_VARIABLE
    Other wise there are a host of options for checking permissions.

  3. #3
    Just Joined!
    Join Date
    Jun 2005
    Location
    San Francisco, CA
    Posts
    54
    Quote Originally Posted by bigtomrodney View Post
    It depends on the context. Are you looking for a specific string or just say the third character of the string for example.

    To just check for a character you could use grep
    Code:
    grep A $(command_to_be_checked)
    grep A $SOME_VARIABLE
    Other wise there are a host of options for checking permissions.
    Cool, thanks BTR. From its man page I couldn't tell that grep could check strings, I thought it would always interpret any string after the options and character as a filename. Btw, how is grep able to know not to look for the character in a file in the current directory by that name?

  4. #4
    Linux Guru
    Join Date
    Nov 2004
    Posts
    6,110
    grep requires a file name to search, or a wildcard/globbing. So
    Code:
    grep something somefile.txt
    Most Unix commands can read from standard input rather than a file, as Unix treats everything as a file. For a lot of commands however you need to specify a hyphen for standard input
    Code:
    grep something -
    In the cases above the command you want to run is evaluated because of the $(somecommand) and thus this is the input for grep. This is the more modern way, in older scripts you will see the back ticks `somecommand`.

    An alternative would be
    Code:
    somecommand |grep something
    This is piping output to grep (standard input).

    If you are interested check out the Advanced Bash Scripting guide at tldp.org - you can read it online or download the PDF. It's well worth the read if you will be using command line at all.

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Seren View Post
    I feel I should know this but I"m drawing a blank:

    How do you find a character in a string that's from a variable or from a command you've just run (`command`)? Is there a single shell command that does this?
    you can also use case
    Code:
            case "$str2" in
            *${str1}*)   echo string1 in string2 ;;
            *)              echo string1 not present ;;
            esac

  6. #6
    Just Joined!
    Join Date
    Jun 2005
    Location
    San Francisco, CA
    Posts
    54
    BigTom and GhostDog, thanks for your help. I'll implement one or both of these when I get the next chance.

  7. #7
    Just Joined!
    Join Date
    Jun 2005
    Location
    San Francisco, CA
    Posts
    54
    It looks like my question has evolved a bit:

    Is there a way test for a string within a variable, so that the test can be used in an if statement? I'm trying to have it all happen in the if statement, not by running commands and then doing RESULT_VARIABLE=$? previous to the if statement.

  8. #8
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16
    hi thr....

    I'm a newbie so my answer might not be so efficient..

    let us say that u wanna check for the string "ss" in a variable '$line':
    then one can do this:

    if [ "${line##*ss*}" != "$line" ]; then
    echo \'ss\' is found
    else
    echo \'ss\' is not found
    fi

    cheers

  9. #9
    Just Joined!
    Join Date
    Jun 2007
    Location
    India
    Posts
    16
    Or if you want it to be generalized then

    $str=ss
    if [ "${line##*$str*}" != "$line" ]; then
    echo $str is found
    else
    echo $str is not found
    fi

  10. #10
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can use case
    Code:
    # line="lkjsflsltestlksjflsd"
    # v="test"
    # case $line in *$v*) echo "found";; *) echo "not found";; esac

Page 1 of 2 1 2 LastLast

Posting Permissions

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