5

Splitting Strings Natively with the Shell: Native vs Native

http://bashcurescancer.com

Splitting Strings Natively with the Shell: Native vs Native

In my previous post on why to split strings with bash itself, I used set to split the string.

This was much faster than using a sub-shell and awk or cut. However, we can do better! The read command accepts a list of variables to split the input. Combined with setting a per command variable, we can write an even more elegant solution.

The magic is here:

while IFS=: read username x uid gid gecos home shell

We set IFS=: only for the execution of read, so there is no need to reset it once done splitting the string. Second we read each field (separated by : via IFS) into a variable directly.

Below is the script we will use to compare the two methods. You will notice I had to up the iterations to 100 in order to see a difference in execution speed:

[root@sandbox ~]# cat ifs-test2.sh
#!/bin/bash
split_words_native() {
# execute 100 times
for i in {0..100}
do
while read line
do
oldIFS=$IFS
IFS=:
set -- $line
IFS=$oldIFS
# at this point $1 is the username, $3
# is the uid, and $7 is the shell
if [[ $3 -gt 10 ]] && [[ '/sbin/nologin' == "$7" ]]
then
echo $1
fi
done < /etc/passwd
done
}

split_words_native_read() {
# execute 100 times
for i in {0..100}
do
while IFS=: read username x uid gid gecos home shell
do
if [[ $uid -gt 10 ]] && [[ '/sbin/nologin' == "$shell" ]]
then
echo $username
fi
done < /etc/passwd
done
}
echo "---Native---"
time split_words_native >/dev/null
echo -e "\n---Read---"
time split_words_native_read >/dev/null

Using read is more elegant and a little faster:

[root@sandbox ~]# ./ifs-test2.sh
---Native---

real 0m0.179s
user 0m0.168s
sys 0m0.010s

---Read---

real 0m0.147s
user 0m0.135s
sys 0m0.012s


Read »
Created by mr-Z 2 years 14 weeks ago – Made popular 2 years 14 weeks ago
Category: Tech   Tags: