Linux Shell Command Learning notes (ii)

Source: Internet
Author: User
Tags diff diff patch save file

    • <cut>

Function: Extract various fields from the output of the input file or command

Usage: cut–c{string range}–d{field separator}–f{field index number}

Example:

(1) View online users: Who | Cut–c1-8

(2) List of user names obtained from System file/etc/passwd: Cut–d:-f1/etc/passwd

ls d4c  Desktop  Documents  Downloads  Music  Pictures  process  process1  process2  Public  Templates   ls cut -c1-5d4cdesktdocumdownlmusicpictuproceproceprocepublitemplvideo
    • <paste>

Role: Merging multiple domains

Usage: paste–d{between field delimiters}

Example:

Paste–d ': ' filename1 filename2

[Email protected] d4c]$Cat 2. Cjust fortest![email protected] d4c]$Cat 1. C#include<stdio.h>intMain () {printf ("Hello world!\n"); Return0;} [email protected] d4c]$ paste-D':' 1. C2. C#include<stdio.h>:just fortest!intMain () {: printf ("Hello world!\n");: Return0;:}:
    • <sort>

Role: Sort

Usage:

-t specifies a delimiter, the default is a space

-R arranged in descending order

-U Remove Duplicate rows

-D is arranged in dictionary order, including letters, numbers, symbols, etc.

-N is arranged in numerical order

+positon1-positon2 from the Position1 field to the Position2 field, including Position1, not including Position2. Positon1 starting from 0.

-K keydefinition Specifies the sort key. The format of the keydefinition option is:

[Fstart [. Cstart] [Modifier] [, [fend] [. Cend] [Modifier]]

The sort key includes any fields specified with the Fstart variable and the Cstart variable that begins with the column specified indicators the field specified by the fend variable and

The cend variable specifies the character of the end of the column. The value of the Modifier variable can be B, D, F, I, N, or R. Modifiers and flags of the same letter are equivalent.

Example:

(1) Sort the third item of the/etc/passwd file (userid): sort-t:-K 3n/etc/passwd

(2) Sort/etc/hosts files based on IP address: sort-t. -K 1,1n-k 2,2n-k 3,3n-k 4,4n/etc/hosts

    • <uniq>

Role: to de-weigh the data

To use: Sort the data first, then go back to the

-D: Output Duplicate rows

-C: Count the data

Example:

Find duplicate user names in the/etc/passwd file: sort/etc/passwd | Cut–f1–d: | Uniq–d

    • <sed>

Role: Editing data

Usage:

sed command file

-N option: Specifies the line number or line number range, or any row if not specified, and p for printing

Example:

Sed–n ' -1,2p ' file.txt #显示前两行

Sed–n '/unix/p ' filename #显示包含 "UNIX" line

d Command: Delete data

Example:

Sed ' 1,2d ' intro #删掉前两行

Sed '/unix/d ' intro #删掉包含 "UNIX" line

s command: Replace

Example

Sed ' s/unix/unix/g ' intro > Temp #将文件intro中 "UNIX" replaced with UNIX and saved the results to the temp file

    • <vi>

(1) Cursor movement

NG: Cursor moves to the beginning of nth

n+: The cursor moves down n rows

N: Move the cursor up n rows

n$: Cursor moves to end of Nth line

(2) Delete command

Do: Delete to the beginning of the line

d$: Delete to end of line

NDD: Deletes the current line and its subsequent n-1 rows

: n1,n2 d: Delete the contents of N1 rows to N2 rows

(3) Search and replace

/pattern: Searches for pattern at the end of the file from the beginning of the cursor

? pattern: Searches for pattern from the beginning of the cursor to the top of the file

: s/p1/p2/g: Replaces all P1 in the current row with P2

: n1,n2s/p1/p2/g: All P1 in line N1 to N2 are replaced with P2

: g/p1/s//p2/g: Replace all P1 in the file with P2

(4) Copy and paste

YY: Copy the current line, move the cursor to a line, p paste

N1,N2 Co N3:N1 is the starting line, N2 is the end line, N3 is the Paste line (where co is the short name of copy, can also be replaced directly with copy)

(5) File Save and exit

: w: Save current file

: x: Save current file and exit

: q!: Do not save file and Exit VI

(6) Copy and paste between multiple files

Start with the Logo Mk (note: M is the command to do the callout, the syntax is m[letter],[letter] for the label of the line)

Then use "Ay K" at the end (the cursor automatically returns to the beginning, and you have put your content in the buffer)

Where a means buffer a

Then escape to your file B with an ex and paste it with the AP command.

namely: VI 1.txt 2.txt? Mk? " Ay k?ex 2.txt? " Ap

(7) VI Segmentation window

: Split two.c: Open another window and edit another specified file with this window

: Vsplit: Vertical Split window

Ctrl-w: Jump Window

(8) Compare the differences between the two files

Vimdiff 1.txt 2.txt

    • <diff> and <patch>

(1) Diff command

The function is used to compare the two files, and then record them, that is, the so-called diff patch

Options

-R is a recursive option

The-u option creates a patch file in a uniform format that is more compact than the default format.

(2) Patch command

Patch is the use of diff-made patches to achieve source file (clip) and destination file (clip) conversion.

Options:

-P0 option to find the destination file (clip) from the current directory

-P1 option to ignore the first level of the directory, starting from the current directory to find.

The-e option indicates that if an empty file is found, delete it

The-r option indicates that the "new" file and the "old" file in the patch file are now switched over (in effect, the new version is patched to make it the old version)

Example:

Single File:

Diff–un from-file To-file >to-file.patch

Patch–p0 < To-file.patch

Patch–re–p0 < To-file.patch

Multiple files:

Diff–unr FROM-DOCU TO-DOCU >to-docu.patch

Patch–p1 < To-docu.patch

PATCH–R–P1 <to-docu.patch

    • <find>

Find Pathname-options [-print-exec-ok ...]

(1) meaning explanation

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.

The-print:find command outputs the matched file to standard output.

The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {} \;, note the space between {} and \;

-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

(2) Options

-name: Find files by file name

-mtime-n +n: According to the file change time to find the file, –n indicates that the file change time is less than n days, + N means the file change time is now N days ago

-newer file1! File2: Find changed time than file File1 new but older than file File2 file

-type: Find a file of a certain type, such as: B – block device file, D – directory, C – character device file, p – pipeline file, L – Symbolic link file, F – Plain file

-depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories

(3) Xargs

When the-exec option of the Find command processes a matching file, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

(4) Example

Using the ls-l command to list the matching files, you can place the Ls-l command in the-exec option of the Find command

Find. -type f-exec ls-l {} \;

Find all files in the system with a file length of 0, and list their full paths;

Find/-type f-size 0-exec ls-l {} \;

Use the grep command to search for the word hostname in all common files:

Find. -type F-print | Xargs grep "hostname"

    • Mathematical calculations

(1) Expr

Expr can be used to calculate the values of various expressions, which can contain strings, logical operations, and functions (but no exponential or logarithmic functions).

Addition: answer= ' expr $c + $d '

Subtraction: answer= ' expr $c – $d '

Multiplication: answer= ' expr $c \* $d '

Subtraction: answer= ' expr $c/$d '

(2) Shell internal operations

The shell itself can be mathematically calculated, and the benefit of using shell intrinsics is to reduce external memory usage by not invoking external programs.

Addition: answer=$ (($c + $d))

Subtraction: answer=$ (($c – $d))

Multiplication: answer=$ (($c * $d))

Subtraction: answer=$ (($c/$d))

(3) BC Calculator

The first two mathematical methods are concentrated on the integer calculation, and BC is a calculator to achieve arbitrary precision, you can set the scale (decimal digits after the decimal digit), to achieve the desired accuracy results.

Addition: Answer= ' echo ' $c + $d "|BC"

Subtraction: answer= ' echo ' $c – $d "|BC"

Multiplication: answer= ' echo ' $c * $d "|BC"

Subtraction: answer= ' echo ' $c/$d "|BC"

To set the precision using scale:

Answer= ' echo ' scale=5; $c * $d "|BC"

To use trigonometric functions, you need to activate BC with the-l option to start the Math library:

Answer= ' echo ' scale=5; C ($d) "|bc-l"

Reference:

http://dongxicheng.org/script/linux-shell/

Linux Shell Command Learning Note (ii)

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.