Use Ifs Instead Of Cut

The cost of using the cut(1) program for several consecutive operations is high. In UsingXargsForRepetition, each iteration parameter was separated from the next by a newline. Unfortunately, the read Shell utility cuts on whole line boundaries. We can input lines with read and then split them using cut, separating fields by their delimiters:

 while read line
 do
f=`cut -f1 -d';'`
d=`cut -f2 -d';'`
c=`cut -f4 -d';'`
...
 done
However, this spawns a Shell process for each parameter on each line, and most of the real time will be expended in process creation. It violates intuition to create a process to pick off each parameter. Yet we must create a new process to use cut.

Therefore:

use the InputFieldSeparator (IFS) variable and read/line/set. Use this instead of the above code:

 IFS=';'
 while read f d junk1 c junk2
 do
...
 done
The use of junk2 is important to make sure that fields following 'c' are not put into the value of 'c'.


CategoryUnixShellPattern


EditText of this page (last edited July 14, 2008) or FindPage with title or text search