Bash Shell concludes "three"

Source: Internet
Author: User
Tags repetition

Cat <--Displays the entire contents of a file, connecting multiple files

Head <--Get the contents of the file header

Tail <--Get the contents of a file trailer

grep <--Find specific content in a file

Sed <--powerful stream editor, can do add, delete, change and other operations

awk <--mode scan and processing tool, is a language

Cut <--extract some of the content from the row

WC <--calculates the number of bytes, characters, words, and lines of data

Sort <--Sorting tool

Uniq <--Removal Repetition tool

TR <--Convert, delete, and reduce the characters

TAC <--Prints the contents of the file in reverse order of the line number.

Rev <--reverses the lines in the file




Cat, Concatinate, connects the contents of one or more files sequentially, outputting them to the standard output.


Cat 12.pub <--Display file 12.pub content to the screen

Cat-n 12.pub <--Display the contents of the file and add line numbers

Cat-a file <--prints out some invisible characters and position marks at the same time


# Merging files

Cat 12.pub 13.pub 14.pub 15.pub >/home/czl/.ssh/authorized_keys

cat/etc/* | Wc-c




Head, read the header of the file


Head-n 3/etc/passwd <--Read the previous three lines of the file/etc/passwd

Head-c 3/etc/passwd <--Read the first three bytes of the file/etc/passwd

Head-c 10m/dev/zero > F1 <--Creating a 10M file

Head-n-1 File <--discard the last line of file

Head-c-3 File <--discards the last 3 bytes of a file




tail, reading the tail of the file


Tail-n 3/etc/passwd <--Read back three lines of file/etc/passwd

Tail-c 3/ETC/PASSWD <--reads the back three bytes of a file/etc/passwd

Tail-n +28/etc/passwd <--starts reading from line 28th until the end of the file (27 rows of discarded head)

Tail-c +28/etc/passwd <--reads from the 28th byte until the end of the file (27 bytes of discarded head)

Tail-f/etc/passwd <--trace file tail content changes, commonly used to observe the log file changes, very practical




grep, extracting rows from a file that conform to a certain format


Common options:

-I <--ignoring case

-l <--Output qualified file name

-N <--Displays the line number of the matching record

The total number of records matched by-c <--output

-O <--output matches that part instead of the entire row

-W <--matches the boundary of a word

-e <--using extended regular expressions

-A <--output below (after)

-B <--output above (before)

-C <--Output contexts (context)

-R <--recursion for searching files under directory

-Q <--does not output results, often used for condition testing

-V <--Show results that are not eligible


Drill grep


1. Include the nobody in the/etc/passwd

2. List the nobody in/etc/passwd

3. List nobody in/etc/passwd, case-insensitive

4. Search in root,/etc/sysconfig directory, which file contains the string Autoswap

5. Find the line number of the row where bash is located in the/etc/passwd file

6. Find out how many nobody in/etc/passwd are not case-sensitive

7. Find out how many lines in the/etc/passwd file contain the word bin

8. Print out the line containing GDM in the/etc/passwd file while typing the following 2 lines

9. Find the row in/etc/passwd that does not contain bash




Sed, stream editor, often used in scripts


The basic format contains the following sections:

1. Specify the range, not explicitly specified, the default is all rows, can be line numbers, or regular expressions

2. Specify the action, commonly used is D, S, p, I, a

D:delete, deleting

S:substitute, replacing

P:print, Print

I:insert, insert in front

A:append, add at the back



Demonstration:


Delete some rows (3d)

Replace some characters (s/abc/abc/)

Delete some characters (delete is replace empty, s/abc//)

Output only some rows (-n 3,10p)

Add a row in front of a line (3i)

Add a row after a line (3a)

Free insertion via pipe add (sed-e 1i xxx-e 3i yyy)



About the selection of the separator for the Replace command


Command sed ' s/a/a/' in sed to perform the substitution action, replace the first lowercase a with uppercase A, where the delimiter slash/can be replaced by any other character, it should be noted that if the data that needs to be processed contains a delimiter, the character must be escaped, for example:


To replace a slash in the string "/etc/passwd" with an underscore, there are two ways


1. There must be a slash in front of the slash to escape

[Email protected] ~]$ ECHO/ETC/PASSWD | Sed ' s/\//_/g '

_ETC_PASSWD ^^


2. Use separate separators, so there is no need to escape the slash, command writing is more concise

[Email protected] ~]$ ECHO/ETC/PASSWD | Sed ' s#/#_ #g '

_etc_passwd


The scope of the replacement action

s/a/a/<--Replace the first a

s/a/a/g <--Replace All a

S/A/A/3 <--Replace 3rd A

s/a/a/3g <--Replace 3rd to last a


Common options for SED


-N <--Turn off default output action

-R <--using an extended regular expression, after using this parameter

The presentation of regular expressions is more convenient, and it is recommended to use


The following two commands are equivalent, but the commands that enable extended regular expressions are easier to read

Sed '/^.\{5\}root/d ' file

Sed-r '/^. {5} root/d ' File


-I <--sed defaults to outputting the modified data to the standard output without modifying the original file

With the-i parameter, sed directly writes the result of the modification to the original file, be careful!




awk, the more common feature is to split a row of data by a single delimiter and output some fields (columns)


Common options:

-F <---field separator, can be a character or multiple characters


How to represent a field:

$ <---entire record

<---1th column (field)

$ <---2nd column (field)

$n <---nth column (field)

$NF <---last column


Internal variables commonly used by awk

FS, field separator, fields separator. field separators can be specified in awk in addition to the command line option-F.


NR, number of records, total rows processed until the current line




Cut, which functions like awk, but not as powerful and complex as awk, when it comes to outputting data in columns, awk is often used, with no cut.


Common options

-D <--defines the separator for the field, which is tab (Delimeter) by default

-F <--Output specified fields (field)

-B <--output byte (byte) at the specified position

-C <--Output the character at the specified position (character), other than-B




WC, count the number of bytes, characters, number of words, number of lines of data


Common options

-C <--Count bytes

-M <--Count of characters

-W <--Count of words

-L <--Count of rows




Sort, sorting files by row


Common options

-U <--removing duplicate rows

-N,-H <--Sort by value

-T <--specifying a separator

-K <--Specify a sorted field

-R <--in reverse order


Sort the files by size:

Ls-l | Sort-k5,5nr



Uniq, removing successive lines of repetition


Common options

-C <--Count of duplicate rows




TR, convert, delete, reduce the same character


Common options

-D

-S




The TAC, which connects the contents of one or more files sequentially, outputs the output to the standard output, and the contents of each file are printed in reverse order by line number.




Rev, reverse the lines in the file


Bash Shell concludes "three"

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.