Find the answer to your Linux question:
Results 1 to 7 of 7
forgive me if this is simpler than i'm recognizing. i haven't been able to find anything that would point me in the right direction as to where to begin with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    3

    bash shell string manipulation


    forgive me if this is simpler than i'm recognizing. i haven't been able to find anything that would point me in the right direction as to where to begin with this.

    i'm looking for a command to add to my shell script that will examine a folder name and return the longest word. that may be vague, so here's an example or two:

    "This is the name of the folder" returns -> "folder"
    "Directory" returns -> "Directory"
    "Folder Name" returns -> "Folder"
    ...and so on.

    also, now that i'm typing this out loud, i realize that an instance might occur where all of the words are the same length, i.e. "Some name"...in which case the first word would do nicely.

    i'm not unfamiliar with assigning file/folder names to variables, it's just what to do once it's in there to pull out the longest word. keep in mind that the spaces in the directories above could easily be underscores, or something of the like.

    any thoughts?

    thanks in advance.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Use sed to change any underscores to spaces, a bash for loop to iterate through the words, a bash if statement to compare two quantities, and bash assignment (in the normal equals-sign way) to assign one value to another. Google
    Code:
    bash string length
    to determine the length of a string.

    I'd go into more detail, but this sounds like homework. And even if it's not homework, you now know where to take this, right?

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    3
    Quote Originally Posted by wje_lf View Post
    Use sed to change any underscores to spaces, a bash for loop to iterate through the words, a bash if statement to compare two quantities, and bash assignment (in the normal equals-sign way) to assign one value to another. Google
    Code:
    bash string length
    to determine the length of a string.

    I'd go into more detail, but this sounds like homework. And even if it's not homework, you now know where to take this, right?
    haha, no, not homework...though i guess it could sound homework-ish. i'm definitely way too old for school. well, unless it's medical school or something along those lines...but, i digress...

    basically, you're saying to iterate through the entire name, searching for the separators (be it space or underscore), assign full words to variables as i come across them and compare them all to find the longest one?

    i think the issue i'm having is what command to use to locate and pull out those individual words. i think i could throw this together in about 30 seconds in VB with instr() and mid().

    unfortunately, since i'm not at work and not programming in VB, my brain can't locate a similar method. i'm definitely not asking for someone to do this for me, just a hint.

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    3
    ah, nevermind...i did the google search you recommended and the first match seems to be pretty helpful. either i guess i was searching on the wrong criteria or i'm becoming an idiot user.

    thanks for your help.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    i think i could throw this together in about 30 seconds in VB with instr() and mid().
    Whoa, whoa, WHOA! :)
    Code:
    #!/bin/bash
    
    while true
    do
      read
    
      REPLY=$(echo $REPLY | sed -e 's/_/ /g')
    
      big_length=0
    
      for xxx in $REPLY    # no instr(), no mid()
      do
        new_length=${#xxx}
    
        if [ $big_length -lt $new_length ]
        then
          big_length=$new_length
          answer=$xxx
        fi
      done
    
      echo $answer
    done
    i'm becoming an idiot user.
    We're all bozos on this bus.

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    bash has its own built in string manipulations capabilities
    Code:
    v="This is the name of the folder"
    set -- $v
    for items in $@
    do   
         echo "item: $items, length: ${#items}"
    done
    output:
    Code:
    # ./test.sh
    item: This, length: 4
    item: is, length: 2
    item: the, length: 3
    item: name, length: 4
    item: of, length: 2
    item: the, length: 3
    item: folder, length: 6
    i leave it to you to do the rest.

  7. #7
    Just Joined!
    Join Date
    Aug 2010
    Posts
    1

    String manipulation with oobash

    If you use bash 4.x you can simply source the oobash. A string lib written in bash with oo-style.
    It has an embedded help system and special support for german umlauts (if nedded).

    Look for oobash on sourceforge and enjoy the feeling of object orientation on bash.

    String is the constructor function:

    String a abcda
    a.length
    5
    a.indexOf a
    0
    a.lastIndexOf a
    4
    a.indexOf da
    3

    There are many "methods" more to work with strings in your scripts:

    -base64Decode -base64Encode -capitalize -center
    -charAt -concat -contains -count
    -endsWith -equals -equalsIgnoreCase -reverse
    -hashCode -indexOf -isAlnum -isAlpha
    -isAscii -isDigit -isEmpty -isHexDigit
    -isLowerCase -isSpace -isPrintable -isUpperCase
    -isVisible -lastIndexOf -length -matches
    -replaceAll -replaceFirst -startsWith -substring
    -swapCase -toLowerCase -toString -toUpperCase
    -trim -zfill

Posting Permissions

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