linux - How to locate files within my computer containing strings of text

07
2014-04
  • user2768038

    What I am trying to do:

    I am trying to locate files with in the directory \\xxxx that contain strings of text DSN=, Server=, UID=, Pwd=, Password=.

    The files do not have to contain every single string but at least two of them.


    The files also have to be .config, .asp or .inc.


    How could I go about locating these files? Do I need software, a certain coding language, etc..

  • Answers
  • Peter

    Basic method to search for string is to use grep. Something like "grep "DSN=" \xxxx" would search for the first string. To use both, you can pass -E: "grep -E ' DSN=|Server=' \xxxx"


  • Related Question

    linux - How to get the shortest string in a text file with shell
  • SpawnST

    Assume I have a text file as below

    abcd
    aaaaaaa
    gfgk
    hahahahahahhahh
    gf
    

    Then gf would be returned.Any good ideas?


  • Related Answers
  • nik

    Assuming your lines each contain a 'word' of characters,
    and, we don't mind letting the shell do a little more work,
    Here is a AWK solution.

    # Let your text be in `str.txt`
    
    awk '{print length($1), $1}' str.txt | sort -nk 1 | head -1
    
    # Output: 2 gf ## Which is the shortest string
    

    You can optimize this to avoid a sort with some more AWK.
    You can tweak this further if you have more than one 'word' per line.

    Also note that if you have multiple shortest strings, this will give you one of them.
    You can do some more tricks to get them too.

  • Bkkbrad

    Awk is great for this:

    awk '(NR == 1 || length < length(shortest)) { shortest = $0 } END { print shortest }'
    

    The first part sets the "shortest" variable to the current line if it is the first line or if the length is shorter than the shortest line seen previously. Finally, the last part prints out the value of shortest.

  • Ignacio Vazquez-Abrams

    BASH FAQ entry #1 tells how to read a file line by line. ${#foo} will give you the length of $foo. Just loop, testing each line in turn.

  • jfgagne

    A solution using sed, and keeping the 1st shortest line from the file:

    sed -e '1h;H;g;s/[^\n]/#/g;s/\(#*\)\n\1/\n/;G;/^\n/s/\n.*\n\(.*\)\n.*/\1/;s/.*\n//;h;$!d' your_file
    

    To keep the last shortest line from the file:

    sed -e '1h;G;h;s/[^\n]/#/g;s/\(#*\)\n\1/\n/;G;/^\n/s/\n.*\n\(.*\)\n.*/\1/;s/.*\n//;h;$!d' your_file
    

    Bellow is an explained version of the 1st shortest line in the form of a sed script file that can be run using sed -f script your_file:

    # The hold space will contain the shortest line at the beginning and the ending of each cycle.
    # The 1st line is the shortest, so put it in the hold space so an empty line will not be returned.
    1h
    # Append the current line to the shortest so far, remember these 2 lines in the hold space, and take a copy in the pattern space to work on them.
    H;g
    # Replace all chars by #.
    s/[^\n]/#/g
    # Delete the same number of # before and after the line delimiter.
    s/\(#*\)\n\1/\n/
    # Append the 2 lines remembered in the hold space to the pattern space.
    G
    # If the hold space begin by a '\n', the current line was shorter, so keep it.
    /^\n/s/\n.*\n\(.*\)\n.*/\1/
    # Else, the previous line was shorter, so keep it.
    s/.*\n//
    # Remember shortest in hold space.
    h
    # If last line, print it (delete everything else).
    $!d