Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a file with linux's AWK

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces how to use linux's AWK to create files related to knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe that everyone after reading this article on how to use linux's AWK to create files will have a harvest, let's take a look at it.

Awk processing: each line is processed in turn, and then output, different from sed: awk reads to memory.

The default delimiter is a space or the tab key

Case 1:

BEGIN {} {} END {} filename

Line processing front line processing line post-processing file name

# awk 'BEGIN {print 1 ok 2} {print "ok"} END {print "- -"}' / etc/hosts

Or:

[root@awk ~] # cat / etc/hosts | awk 'BEGIN {print 1 + 2} {print "ok"} END {print "-"}'

0.5

Ok

Ok

-

Records and fields

Awk assumes that its input is structured rather than an irregular string of characters. By default, it takes each input line as a record and words separated by spaces or tabs as fields.

Awk is treated as a record: a line is a record, because the string separated by a newline character by default in awk is a record. (default\ nnewline character: record delimiter)

Record: a string split by a record splitter defaults to newline\ n

Field: a string split by a field separator defaults to single or multiple "" tab keys.

= =

Variables in awk:

$0: represents the entire line

NF: number of statistical fields

$NF: it is number finally, which represents the information of the last column

RS: enter record delimiter

ORS: output record delimiter.

NR: print record number, (line number)

FNR: you can separate and print line numbers according to different files.

FS: enter the field delimiter, which defaults to a space.

The field delimiter of the OFS output, default to a space.

FILENAME filename the name of the file to be processed

$1 first field, $2 second field, and so on.

Case study:

FS (input field delimiter)-generally abbreviated to-F (before line processing)

[root@awk ~] # cat / etc/passwd | awk 'BEGIN {FS= ":"} {print $1}'

Root

Bin

Daemon

Adm

Lp

Sync

Shutdown

Halt

Mail

Operator

Games

[root@awk ~] # cat / etc/passwd | awk-F:'{print $1}'

Root

Bin

Daemon

Adm

Lp

Sync

Shutdown

Halt

Mail

Operator

Note: if-F is not added by default, it is distinguished by space!

=

OFS (output field delimiter)

[root@awk ~] # cat / etc/passwd | awk 'BEGIN {FS= ":"; OFS= ".."} {print $1 drawing 2}'

Root..x

Bin..x

Daemon..x

Adm..x

Lp..x

Sync..x

Shutdown..x

Halt..x

Mail..x

=

1. Create two files

[root@awk ~] # vim a.txt

Love

Love.

Loove

Looooove

[root@awk ~] # vim file1.txt

Isuo

IPADDR=192.168.246.211

Hjahj123

GATEWAY=192.168.246.1

NETMASK=255.255.255.0

DNS=114.114.114.114

NR represents the record number, and when awk records the behavior, this variable is equivalent to the current line number

[root@awk ~] # awk'{print NR,$0} 'a.txt file1.txt

1 love

2 love.

3 loove

4 looooove

five

6 isuo

7 IPADDR=192.168.246.211

8 hjahj123

9 GATEWAY=192.168.246.1

10 NETMASK=255.255.255.0

11 DNS=114.114.114.114

FNR: represents the record number, and when awk records the behavior, this variable is equivalent to the current line number (separate files)

[root@awk ~] # awk'{print FNR,$0} 'a.txt file1.txt

1 love

2 love.

3 loove

4 looooove

five

1 isuo

2 IPADDR=192.168.246.211

3 hjahj123

4 GATEWAY=192.168.246.1

5 NETMASK=255.255.255.0

6 DNS=114.114.114.114

=

RS (enter record delimiter)

1. Create a file

[root@awk ~] # vim passwd

Root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin

[root@zabbix-server ~] # cat passwd | awk 'BEGIN {RS= "bash"} {print $0}'

Root:x:0:0:root:/root:/bin/

Bin:x:1:1:bin:/bin:/sbin/nologin

ORS (output record delimiter)

two。 Make changes to the file just now

[root@awk ~] # vim 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

[root@awk ~] # cat passwd | awk 'BEGIN {ORS= "} {print $0}'

Root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

=

NF: number of statistical columns

[root@awk ~] # cat / etc/passwd | awk-F:'{print NF}'

seven

seven

seven

seven

$NF: print the last column

[root@awk ~] # cat / etc/passwd | awk-F:'{print $NF}'

/ bin/bash

/ sbin/nologin

/ sbin/nologin

/ sbin/nologin

/ sbin/nologin

Exercise 1: merge files into one line

[root@awk ~] # cat / etc/passwd | awk 'BEGIN {ORS= ""} {print $0}'

Exercise 2: divide a line into multiple lines

1. First create a file

[root@awk ~] # vim d.txt

Root:x:0:0:root:/root:/bin/bash

[root@awk ~] # cat d.txt | awk 'BEGIN {RS= ":"} {print $0}'

Root

X

0

0

Root

/ root

1. Print the second and third columns in a file

# cat / etc/passwd | awk-F:'{print $2 recording 5}'

two。 Prints a character of the specified column in the specified row

# free-m | awk 'NR==2 {print $2}'

3. Count the number of lines in a file

# cat / etc/passwd | awk'{print NR}'

4. Using if conditional judgment in awk

+ + I: add from 1, and assignment is in operation

ISuppli: add from 0, and the operation is assigning

If statement:

{if (expression) {statement; statement;.}}

Show administrator user name

# cat / etc/passwd | awk-F:'{if ($3 million / 0) {print $1 "is administrator"}}'

Statistical system users

# cat / etc/passwd | awk-F:'{if ($3 > = 0 & & $3)

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report