Added better split method
This commit is contained in:
42
README.md
42
README.md
@@ -15,8 +15,8 @@ src="https://img.shields.io/badge/license-MIT-blue.svg"></a>
|
|||||||
<br>
|
<br>
|
||||||
|
|
||||||
The goal of this repository is to document known and unknown methods of
|
The goal of this repository is to document known and unknown methods of
|
||||||
doing various tasks using only built-in bash features. Using the snippets
|
doing various tasks using only built-in `bash` features. Using the snippets
|
||||||
from this guide can help to remove unneeded dependencies from your scripts
|
from this bible can help to remove unneeded dependencies from your scripts
|
||||||
and in most cases make them that little bit faster. I came across these
|
and in most cases make them that little bit faster. I came across these
|
||||||
tips and discovered a few while developing
|
tips and discovered a few while developing
|
||||||
[neofetch](https://github.com/dylanaraps/neofetch),
|
[neofetch](https://github.com/dylanaraps/neofetch),
|
||||||
@@ -28,7 +28,7 @@ written where applicable. If you're looking to contribute, have a read of
|
|||||||
the
|
the
|
||||||
[CONTRIBUTING.md](https://github.com/dylanaraps/pure-bash-bible/blob/master/CONTRIBUTING.md).
|
[CONTRIBUTING.md](https://github.com/dylanaraps/pure-bash-bible/blob/master/CONTRIBUTING.md).
|
||||||
It outlines how the unit tests work and what's required when adding
|
It outlines how the unit tests work and what's required when adding
|
||||||
snippets.
|
snippets to the bible.
|
||||||
|
|
||||||
If you see something that is incorrectly described, buggy or outright
|
If you see something that is incorrectly described, buggy or outright
|
||||||
wrong, open an issue or send a pull request. If you know a handy snippet
|
wrong, open an issue or send a pull request. If you know a handy snippet
|
||||||
@@ -269,14 +269,40 @@ is_hex_color "$color" || color="#FFFFFF"
|
|||||||
|
|
||||||
This is an alternative to `cut`, `awk` and other tools.
|
This is an alternative to `cut`, `awk` and other tools.
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
split() {
|
||||||
|
# Usage: split "string" "delimiter"
|
||||||
|
IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
|
||||||
|
printf '%s\n' "${arr[@]}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
string="1,2,3"
|
$ split "apples,oranges,pears,grapes" ","
|
||||||
|
apples
|
||||||
|
oranges
|
||||||
|
pears
|
||||||
|
grapes
|
||||||
|
|
||||||
# To multiple variables.
|
$ split "1, 2, 3, 4, 5" ", "
|
||||||
IFS=, read -r var1 var2 var3 <<< "$string"
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
|
||||||
# To an array.
|
# Multi char delimiters work too!
|
||||||
IFS=, read -ra vars <<< "$string"
|
$ split "hello---world---my---name---is---john" "---"
|
||||||
|
hello
|
||||||
|
world
|
||||||
|
my
|
||||||
|
name
|
||||||
|
is
|
||||||
|
john
|
||||||
```
|
```
|
||||||
|
|
||||||
## Change a string to lowercase
|
## Change a string to lowercase
|
||||||
|
|||||||
5
test.sh
5
test.sh
@@ -151,6 +151,11 @@ test_extract() {
|
|||||||
rm test_file
|
rm test_file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_split() {
|
||||||
|
IFS=$'\n' read -d "" -ra result < <(split "hello,world,my,name,is,john" ",")
|
||||||
|
assert_equals "${result[*]}" "hello world my name is john"
|
||||||
|
}
|
||||||
|
|
||||||
assert_equals() {
|
assert_equals() {
|
||||||
if [[ "$1" == "$2" ]]; then
|
if [[ "$1" == "$2" ]]; then
|
||||||
((pass+=1))
|
((pass+=1))
|
||||||
|
|||||||
Reference in New Issue
Block a user