• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bash Shell and string.split  RSS feed

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Bash Shell in Suse 10. I want to write a function like string.split in Java .

Anybody could help ? Please just use Bash Shell.

Thanks.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:

1st example: Split with default IFS (Internal Field Separator, which is space/tab/new line)
2nd example: Use sed to split.
3rd and 4th example: Reset IFS to your needs:
 
Jim Hertzler
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#!/bin/bash

# Split the command line argument on the colon character.

SaveIFS=$IFS
IFS=":"
declare -a Array=($*)
IFS=SaveIFS

echo "Array[0]=${Array[0]}"
echo "Array[1]=${Array[1]}"
echo "Array[2]=${Array[2]}"
echo "Array[3]=${Array[3]}"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!