Pages

Bash Script efficiently

The part techy kinds lives of each computer is letter of indices. Face leaves it, which is we all unprofessional programmers at the heart. Its part of our desire to steer to the electronic brain which takes up as much from our time. To the anger and to fear of our other halves:) An index to write is another way of programming fairly and there so the same guidelines and guidelines to follow should, which became you, when, a program in C, c++, fundamentally, writing Java.net, mono, or whatever there different language is

There are something advantages, if one writes indices, which form to complete it fast up more simply rustle some lines, around a repetitive task. Most the time of this refers for:next and while:do loops also. The designations, which are used, can be different, but use the fundamental structure and like them, are the same. Other programs from the indices to to designate is very simple and the upper sections, which are established in the functions, can in its entirety be used, if one manipulates the exit of these programs. This is called frequently adhesive code, since the index, which is fair is used, as a bridge used, to the different programs to stick together. While this all fine and Geck are, it is not the most efficient way. Some the functions that other programs are designated for, can already will be geeinbaut to the upper section, the index lets which run inside. This can have a drastic effect on the speed of the enterprise.

When example a quantity of indices is used, around the machine log book documents of the different computer activities to stampfen. E.G. activities of a Netzinternets can produce very large log files. A day's exit can produce more than twenty megabytes of an individual text document easily. Regarding that you can fit an entire five hundreds side book on an individual floppy Disc (to remind it itself of those?) that represents a quantity of information. These information overloading in something to reduce that the CEO to understand knows requires the use of indices.

Generally this means a reading off of each line and a taking the good material and storing out to another document. Under Linux there are some programs, which can do this. Awk, sed, cut, can line everything be used, in order to manipulate each line and to remove the necessary information. The problem with those is that, since they are external programs there expenses are designating it and they load into memory and they run having. Set into a loop, which lets some thousands run, time sets important on an equal footing ones retarded firmly.

However many upper sections and I use impact upper section here, when example a word expansion have function. This is, where the entrance is defective into different words above and variables assigned over a defined delimitation. Exactly make available the same functionality, awk, sed and the cut also. So, if you need that consider functionality, to use established in upper section functionality. This differentiates time from five minutes down to thirty seconds. Not one to be sneezed on illustration. Here a simple example, straight, is to point to the difference it cannot possibly not work.

cat "somefile.csv"|while read record
do
echo $record|cut -d, -f1-4,6,12 >> smallfile.csv
done

This calls two programs, "cat" to feed the file to the loop and "cut" to save fields 1 to 4,6 and 12 to another file. Because the script calls "cut" so many times it will take an extremely long time on large files. A far quicker method and simpler in my opinion is.

IFS=','
while read month day year time junk junk junk junk junk junk sourceip junk junk webpage
do
echo "$month $day $year,$time,$sourceip,$webpage" >> smallfile.csv
done < somefile.csv
IFS=' '

Not only is the code easier to understand it is far quicker as it doesn't call any external programs. The motto of the story here is work smarter and faster not fancy and slow.