|
We do not let the next generation of Linux and Unix administrators forget initialization scripts and basic tools Benefits
I had time to see a post on Reddit, "how to manipulate text files." This is a very simple needs, like the day we met as usual Unix people. His problem is, how to delete duplicate rows in the file, leaving only the non-repetition. It sounds simple, but when the file is large enough, there will be a bit more complicated.
This problem There are many different answers. You can use almost any language to write such a script, just put in the time and complexity of the code may vary. Depending on your personal level, it will probably take 20-60 minutes. But if you use Perl, Python, Ruby one, you may soon realize it.
Or you can use one of the following methods, make you very heart-warming: only awk.
The answer is by far the most simple, the easiest way to solve the problem. It is a single line!
awk '! seen [$ 0] ++'
Let's see what happens:
In this command, in fact, I hide a lot of code. awk is a text processing language, and it is with a lot of presets. The first thing you see is actually the result of a for loop. awk assumes that you want to loop process each line of the input file, so you do not need to explicitly specify it. awk is also assumed that the data need to print output processing after you, so you do not need to specify it. Finally, awk is assumed that the last sentence cycle instruction execution after the end of this one no longer requires you to specify it.
This example is seen in a string associated with the name of the array. $ 0 is a variable that represents the entire current line. Therefore, this command is translated into human language "for each line of the file to be checked, if you have not seen it, print it out." If a key does not exist in the associative array is added to the array, and to increase its value, so awk next encounter the same row will not match (the condition is "false"), which is not printed.
Some people think this is an elegant, others argue it may cause confusion. Any use awk are in the daily work of the first category. awk is designed to do this. In awk, you can write multiple lines of code. You can even use awk write some disturbing complex functions. But in the end it is, awk is a text-processing program carried out, usually by pipeline. Remove (not necessary) circular definition is common and efficient usage, but if you like, you can also use the following code to do the same thing:
awk '{if print $ 0 (seen [$ 0]!); seen [$ 0] ++}'
This will produce the same results.
awk is a perfect tool for the job done. However, I believe many administrators - especially the new administrator will instead use Bash or Python to accomplish this task, because of its knowledge and understanding of the ability of awk looks over time slowly being forgotten . I think this is a question mark, because of the lack of understanding of previous solutions, those who have solved the problem for decades suddenly appeared.
shell, grep, sed and awk is the foundation of Unix. If you can not be very easy to use them, you will be bound to live their own, because they constitute the command line and scripts to interact with the Unix system foundation. Learn how these tools work is one of the best ways to observe real-life examples and experiments, you can find a lot in various Unix-derived systems initialize the system, but Linux distributions they have been replaced by systemd.
Millions of Unix Shell Scripting and Unix administrators understand how the tools to read, write, modify, and used in the initialization script. Initialization scripts for different systems are very different, even different Linux distributions are also different. But they are derived from sh, and they are used like sed, awk also like grep core command-line tool.
Every day I hear a lot of people complain about the initialization script too "old" and very "difficult." But in fact, the initialization scripts and tools Unix administrators, like every day, but also provides a very good way to become more familiar with and habits of these tools. He said initialization scripts difficult to read and hard to use actually is to admit your lack of familiarity with Unix based tool.
Speaking on Reddit sees, I have encountered this problem from a new entrants to the Linux system administrator, "and asked whether he should have to go to school system initialization old sysvinit". Most of the answers to this post are positive - Yes, we should learn sysvinit systemd and two. One commentator even pointed out that the initialization script is a good way to learn the Bash. The other news is, Fortune 50 company strong has no plans to migrate to systemd-based release.
But that reminds me that this is indeed a problem. If we continue along the way to eliminate from the script and the operating system core components to develop, due to the neglect of the contact, we will inadvertently make the new administrator is difficult to learn the basic Unix tools.
I do not know why someone would want in layer after layer of abstraction to cover internal Unix, but this continues to make possible a new generation of system administrators become just press the button of the workers. I think this is not a good thing. |
|
|
|