In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use awk, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. First of all, a brief summary of some text processing three Musketeers:
Grep families: text filtering tool
Sed: line editing tool
two。 Let's take a look at what awk is.
Awk now uses GNU awk, or gawk, on linux, and awk is now a symbolic link file for gawk on centos systems. So what exactly is awk used for on centos systems? Awk is a report generator, a tool for formatting text output.
3. What is the help of learning awk?
Learning awk, we can easily view the text we want to deal with, etc., to facilitate us to solve problems.
4. The basic usage of gawk:
Gawk-pattern scanning and processing language mode scanning and processing language
Syntax: gawk [options] 'program' file...
To elaborate on the following:
4.1 [options]:
-F indicates the field separator at the time of input
-v VAR=VALUE is mainly used to implement custom variables
4.2 program:PATTERN {ACTION STATEMENTS}
Statements are separated by semicolons
Built-in command: print,printf
5. The variables used in the
5.1Variables (Note: you can't add a $sign to your own variables in awk, which is different from bash)
Built-in variables:
FS:input field seperator input field delimiter, which defaults to blank characters
This is equivalent to the-F delimiter
OFS:output field seperator output field delimiter, which defaults to a blank character
Eg:
[root@bucktan ~] # tail-2 / etc/fstab | awk-v OFS=':''{print $1J 3}'
Sysfs:sysfs
Proc:proc
[root@bucktan] # tail-2 / etc/fstab
Sysfs / sys sysfs defaults 0 0
Proc / proc proc defaults 0 0
[root@bucktan ~] # tail-2 / etc/passwd | awk-v FS=':''{print $1J 3}'
Apple 2000
Zabbix 495
RS:input record sepereator specifies the newline character when entering
ORS:output record sepereator specifies the newline character for output
Eg:
[root@bucktan ~] # awk-v RS=''- v ORS='##''{print}'/ etc/passwd
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
Ftp:x:14:50:FTP##User:/var/ftp:/sbin/nologin
Nobody:x:99:99:Nobody:/:/sbin/nologin
Dbus:x:81:81:System##message##bus:/:/sbin/nologin
Pay attention to the # in it.
NF:number of field counts the number of fields in each row
{print NF}
{print $NF}: print the last field
~] # awk'{print NF}'/ etc/passwd
Eg:
[root@bucktan ~] # awk-F:'{print $NF}'/ etc/passwd
NR:number of record prints the line number of each line, but in fact counts the total number of lines in the file
FNR: count each file and count the lines separately
FILENAME: the name of the file currently being processed
ARGC: the number of command line arguments
ARGV: an array that holds the parameters given by the command line
ARGV [0]: the first parameter
.
Eg:
[root@bucktan ~] # awk 'BEGIN {print ARGV [0]}' / etc/passwd / etc/fstab
Awk
[root@bucktan ~] # awk 'BEGIN {print ARGV [1]}' / etc/passwd / etc/fstab
/ etc/passwd
Note: the purpose of BEGIN is to achieve results that are not printed on each line. If there is no BEGIN, each line will display a printed value
5.2 Custom variables:
(1):-v var=value variable name is case sensitive
(2): define directly in program
Eg:
[root@bucktan ~] # awk-v abc='love you''{print abc}'/ etc/passwd
Another implementation:
[root@bucktan ~] # awk 'BEGIN {abc= "love you"; print abc}'
Love you
Syntax: gawk [options] 'program' file...
Program:PATTERN {ACTION STATEMENTS}
6. Built-in commands in it
6.1:print (used in {ACTION STATEMENTS})
Print iterm1,iterm2, .
Main points: (1): commas are delimiters, but blank characters are displayed instead of commas when output
(2): each output iterm can be a string, numeric value, field of the current record, variable, or expression of awk
(3): if iterm is omitted, it is equivalent to print$0
Eg:
[root@bucktan ~] # tail-5 / etc/passwd | awk-F:'{print "hehe:
Hehe:111tcpdump 72
Hehe:111bucktan 500
Hehe:111centos 496
Hehe:111apple 2000
Hehe:111zabbix 495
[root@bucktan ~] # tail-2 / etc/passwd | awk-F:'{print}'
Apple:x:2000:2000::/home/apple:/bin/bash
Zabbix:x:495:492:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
6.2 printf Command
Printf-format and print
Formatted output: printf FORMAT iterm1,iterm2,...
Main points: (1): FORMAT: must be given
(2): it will not wrap automatically. You need to show the newline control character to know which line.
(3): in FORMAT, you need to specify a format symbol for each subsequent iterm.
Format character:
% c: ASCII code that displays characters
% dline% I: display decimal integers
% eGraine% E: scientific counting method numerical display
% gpeng% G: display values in scientific counting or floating-point form
% s: display string
% u: unsigned integer
%%: show% itself
Style one
[root@bucktan ~] # awk-F:'{printf "% s", $1}'/ etc/passwd
Rootbindaemonadmlpsyncshutdownhaltmailuucpoperatorgamesgopherftpnobodydbususbmuxdvcsarpcrtkitavahi-autoipdabrtrpcusernfsnobodyhaldaemongdmntpapache
Style two
[root@bucktan ~] # awk-F:'{printf "% s\ n", $1}'/ etc/passwd
Root
Bin
Daemon
Adm
Lp
Style three
[root@bucktan ~] # awk-F:'{printf "username:% s\ n", $1}'/ etc/passwd
Username: root
Username: bin
Username: daemon
Username: adm
Username: lp
Style four
[root@bucktan ~] # awk-F:'{printf "username:% s, userid:% s\ n", $1 printf 3}'/ etc/passwd
Username: root, userid: 0
Username: bin, userid: 1
Username: daemon, userid: 2
Username: adm, userid: 3
Username: lp, userid: 4
Username: sync, userid: 5
Username: shutdown, userid: 6
Username: halt, userid: 7
Username: mail, userid: 8
Modifier:
# [. #]: the first number controls the width of the display: the second # represents the precision of the decimal point
Such as% 3.1f
-: align left, do not add-default right alignment
+: symbols that display numeric values
[root@bucktan ~] # awk-F:'{printf "username:%-15s, userid:% s\ n", $1 printf 3}'/ etc/passwd
Username: root, userid: 0
Username: bin, userid: 1
Username: daemon, userid: 2
# # omitting later content
7. Operator
# (also used in execution statements, remember to separate the semicolon from the print built-in command)
Arithmetic operator:
Xquoy, xMelector x* | / | ^ |% y
-x: negative valu
+ x: convert to numeric value
String operators: unsigned operators, string concatenation
Assignment operators: =, +, -, *, /,% =, +,--
Comparison operator: >, =
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.