In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the differences between sed and awk in linux". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the differences between sed and awk in linux?"
Differences: 1, sed is a flow editor, and awk is a text formatting tool, report generator; 2, awk is suitable for file extraction and finishing, sed is suitable for editing files; 3, sed reads a line of data, it will be processed in terms of rows, while awk reads a line, it will be cut into fields and processed in detail in terms of fields (columns).
The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.
Awk
Awk is a program language, which has a strong function in dealing with documents. Awk is good at extracting data from formatted messages or from a large text file.
The awk command scans the file line by line (from line 1 to the last line), looking for the line containing the target text, and if the match is successful, the user's desired action is performed on the line; otherwise, no processing is done on the line.
The basic format of the awk command is:
Awk [option] 'script Command' filename
The options commonly used for this command and their respective meanings are shown in Table 1.
Table 1 awk command options and meaning options meaning-F fs specifies that the input line delimiter is fs, and the default delimiter for the awk command is a space or tab. -f file reads awk script instructions from a script file instead of entering instructions directly on the command line. -v var=val sets a variable var and gives its device an initial value of val before executing the process.
The power of awk lies in the script command, which consists of two parts, matching rules and executing commands, as follows:
'match rule {execute command}'
The matching rule here, which works the same as the address part of the sed command, is used to specify that script commands can act on specific lines in the text content, either using strings (such as / demo/, for viewing lines containing demo strings) or regular expressions. It is also important to note that the entire script command is enclosed in single quotation marks (''), while the execution part of the command needs to be enclosed in curly braces ({}).
When the awk program executes, if the execution command is not specified, the matching lines are output by default; if no matching rules are specified, all lines in the text are matched by default.
Take a simple example:
[root@localhost ~] # awk'/ ^ $/ {print "Blank line"} 'test.txt
In this command, / ^ $/ is a regular expression that matches blank lines in the text, and you can see that the command is executed using the print command, which is often used, and its simple function is to output the specified text. Therefore, the function of the whole command is that if test.txt has N blank lines, executing this command will output N Blank line.
Sed
The sed command uses stream editing mode, and the most obvious feature is that before sed processes the data, it needs to provide a set of rules in advance, and sed will edit the data according to this rule.
Sed processes the data in the text file according to script commands, which are either entered from the command line or stored in a text file in the following order:
Read only one line at a time
Match and modify the data according to the rule commands provided. Note that sed does not modify the source file data directly by default, but copies the data to the buffer, and the modification is limited to the data in the buffer.
The result output will be executed.
When a row of data is matched, it continues to read the next row of data and repeats the process until all the data in the file is processed.
The basic format of the sed command is as follows:
Sed [options] [script commands] filename
The common options and meanings of this command are shown in Table 1.
Table 1 sed commands Common options and meaning options meaning-e script commands this option adds the following script commands to existing commands. -f script command file this option adds script commands in subsequent files to existing commands. -n by default, sed automatically outputs the processed content after all the scripts have been specified, and this option masks the startup output, which needs to be completed by using the print command. -I this option modifies the source file directly, so use it with caution.
The key to successfully using the sed command is to master a variety of script commands and formats, which can help you customize the rules for editing files.
The difference between sed and awk
Sed is the stream editor, while awk is the text formatting tool, report generator
If the file is formatted, that is, divided into multiple fields by delimiters, awk takes precedence
Awk is suitable for file extraction and collation, and sed is suitable for file editing.
Awk is suitable for column (domain) operation, and sed is suitable for row operation.
Sed: read one line at a time to deal with, sed is suitable for simple text replacement and search, sed reads a line, in lines as a unit, processing.
Awk: read one line at a time to process (the same as sed), but awk reads a line, cuts it into fields, and handles details in terms of fields (columns).
Thank you for your reading, the above is "what is the difference between sed and awk in linux". After the study of this article, I believe you have a deeper understanding of the difference between sed and awk in linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.