Two. Unit sixth. Shell script commands

Source: Internet
Author: User
Tags diff

#########################
# #二. Unit sixth. Shell Script Command # # # # #
#########################

1.diff
diff File File1 # #比较两个文件的不同
-C # #显示周围的行
-U # #按照一定格式统一输出生成补丁
-R # #比较两个目录中文件的不同

Patch File File.path # #打补丁
-B # #备份原文件

2.grep
grep Key Character File | Directory # #在文件或目录中查找含有关键字的行

Grep-i # #忽略大小写
-N # #显示关键字所在行
-C # #显示过滤结果的个数
-V # #反向过滤
-e "keyword 1| keyword 2" # #过滤多个关键字
-R Directory # #在目录中查找含有关键字的文件
Note: ^ keyword # #以关键字开头
Keyword $ # #以关键字结尾

3.cut
Cut # #截取字符
cut-d Delimiter # #指定分隔符
Cut-f 1,7 # #显示指定的列
Cut-c 1-4 # #显示指定的字符

4.sort # #排序
Sort
-N # #纯数字排序
-U # #去冗余
|UNIQ-C # #去除冗余并统计冗余次数
-T # #指定分隔符
-K # #指定列

5.uniq
Sort file |uniq-c # #去除冗余并统计冗余次数
|uniq-d # #显示冗余行
|uniq-u # #显示唯一行

6,sed
Sed ' s/original character/replacement character/g ' file
Sed-e ' Strategy 1 '-e ' policy 2 ' file
Sed-i File # #把转换后的内容输入到指定文件
Sed ' 3,5s/original character/replacement character/g ' # #3-5 line replacement
Sed xd # #屏蔽指定行
Sed XP # #复制指定行
Sed-n XP # #只显示指定行

Create a user's script
#! /bin/bash
max=$ (Wc-l $ | cut-d ""-F 1)

For NUM in $ (seq $MAX)
Do
username=$ (sed-n ${num}p $)
passwd=$ (Sed-n ${num}p)
Useradd $USERNAME
echo $PASSWD | passwd--stdin $USERNAME
Done

7.awk

Brief introduction

Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.

AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.

Awk has its name from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's surname. In fact, Awk does have its own language: The awk programming language, a three-bit creator has formally defined it as "style scanning and processing language." It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.



How to use

awk ' {pattern + action} ' {filenames}

Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a particular pattern. pattern is the regular expression to be represented, surrounded by slashes.

The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for additional text operations. A complete awk script is typically used to format the information in a text file.

Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.


Call awk

There are three ways of calling Awk
Copy Code

1. Command-line mode
awk [-F field-separator] ' commands ' input-file (s)
Where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is the file to be processed.
In awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without naming the-F domain delimiter.

2.shell Script Mode
Insert all the awk commands into a file and make the awk program executable, and then awk command interpreter as the first line of the script, again by typing the script name to invoke.
Equivalent to the first line of the shell script: #!/bin/sh
Can be replaced by: #!/bin/awk

3. Insert all the awk commands into a separate file and then invoke:
Awk-f awk-script-file Input-file (s)
Where the-f option loads the awk script in Awk-script-file, Input-file (s) is the same as above

Cases:
Show only the IP address of this machine
Ifconfig eth0 | grep inet | grep inet6-v | Awk-f "" ' {print $} '
172.25.0.222

This article from "12100661" blog, declined reprint!

Two. Unit sixth. Shell script commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.