Jump to content

splitting a word

goatedpenguin
Go to solution Solved by Eigenvektor,

Here's an example for a single string passed into the script

STRING=$1

STR_LENGTH=${#STRING}
STR_MIDDLE=$((STR_LENGTH / 2))
STR_REMAINDER=$((STR_LENGTH - STR_MIDDLE))

echo $STR_LENGTH - $STR_MIDDLE - $STR_REMAINDER

echo ${STRING:0:$STR_MIDDLE}
echo ${STRING:$STR_MIDDLE:$STR_REMAINDER}

 

There is a word on each line and while I am doing a while loop over the file line-by-line how do I split the word in half? Example: certification -> certif ication

 

Note this script is in bash. 

 

Thanks in advance 🙂 

Link to comment
Share on other sites

Link to post
Share on other sites

Here's an example for a single string passed into the script

STRING=$1

STR_LENGTH=${#STRING}
STR_MIDDLE=$((STR_LENGTH / 2))
STR_REMAINDER=$((STR_LENGTH - STR_MIDDLE))

echo $STR_LENGTH - $STR_MIDDLE - $STR_REMAINDER

echo ${STRING:0:$STR_MIDDLE}
echo ${STRING:$STR_MIDDLE:$STR_REMAINDER}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks @Eigenvektor, this was for a file so I made a few modifications to your code just to make sure I get the technique down but it works as intended thanks! I however also ended up making a script with a bit of research and a "bit of" help from chatGPT, but your solution looks more efficient, here is my code if you want to take a look:

 

#!/bin/bash

wordchange () {
  # Create directory if it doesn't exist
  mkdir -p /home/$(whoami)/challenge/wordChallenge

  # Create new empty text file
  touch /home/$(whoami)/challenge/wordChallenge/wordChallenge.txt

  # Read file line by line
  while IFS= read -r line; do
    # Check if line contains one or two words
    words=($line)
    if [ ${#words[@]} -eq 1 ]; then
      # Split one word in half and append to new text file
      word=${words[0]}
      length=${#word}
      half_length=$((length / 2))
      first_half=${word:0:half_length}
      second_half=${word:half_length}
      echo "${first_half} ${second_half}" >> /home/$(whoami)/challenge/wordChallenge/wordChallenge.txt
    elif [ ${#words[@]} -eq 2 ]; then
      # Combine two words and append to new text file
      combined="${words[0]}${words[1]}"
      echo "$combined" >> /home/$(whoami)/challenge/wordChallenge/wordChallenge.txt
    fi
  done < $1
}

wordchange $1

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, mariushm said:

You would probably want to split the words after a syllable. So you'd separate into syllables and then cut after the syllable that goes over half the characters.

 

Some rules for separating words into syllables  :

Syllable Rules | Syllable Stress | Algorithm

This was not for syllable splitting it was just an assignment hahaha, though that would be an interesting thing to code up 🙂 
 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×