Linux common word processing command summary

  

linux grep command

function grep command in Linux system is a powerful text search tool, it can use regular expressions to search for text, and print the matching lines . The full name of grep is Global Regular Expression Print, which represents the global regular expression version, and its usage rights are for all users.

Main parameters

-n: Display matching lines and line numbers.

-v: Shows all lines that do not contain matching text.

-I: Does not distinguish between uppercase and lowercase (only for single characters).

-l: When querying multiple files, only the file name containing the matching characters is output.

-e: Use extended regular expressions

-w: Match only the entire word, not part of the string (that is, the whole of consecutive characters, numbers, and underscores)< Br>

Linux sed command

Function

The Sed command is mainly used to process the entire line. For the column processing, use the awk command described below.

Usage sed Option Action File

Common Options:

-n: Use silent mode. In the general sed usage, all data from STDIN will generally be listed on the screen. However, if the -n parameter is added, only the line (or action) that has been specially processed by sed will be listed. -f: Write the action of sed directly in a file, -f filename to execute the sed action in filename; -i: directly modify the contents of the read file instead of being output by the screen.

Common Actions

a : Added, a can be followed by a string, and these strings will appear on the new line (current next line), if you want to add multiple lines , you need to use \\ newline c: instead, c can be followed by a string, these strings can replace the line between n1, n2! If the string is to be wrapped, you need to use \\ with d: delete, because it is deleted, so d is usually not followed by any 咚咚; i: insert, i can be followed by a string, and these strings will be on a new line Appears (currently the previous line); if you want to insert multiple lines, you need to use the line w: print, which will also print out a selected item. Usually p will work with the parameter sed -n ~ s : replace, you can directly replace the work 哩! Usually this s action can be combined with regular notation! For example s/old/new/g is it!

Linux awk command

awk can be used as a programming language independently, only a simple part of it is introduced here

awk is generally used with print and printf

For example:

Display the username, user id and group id in /etc/passwd, preceded by the line number (via cat -n) -F is used to specify the separator of the column, $1, $3 , $4 refers to lines 1, 3, 4 respectively

cat -n /etc/passwd |  Awk -F “:” '{print $1 “\\t” $3 “\\t” $4}'


The first line shows the username userid groupid and then displays and last The same content as the example (the line number is displayed by NR in awk)

cat /etc/passwd |  Awk 'BEGIN {FS=”:”; printf “%d\\t%10s\\t%10s\\t%10s\ ”,NR,”username”,”userid”,”groupid”} \\

{printf “%10s\\t%10s\\t%10s\ ”,$1,$3,$4}\\

END {printf “finish\ ”}'< Br>


Show files with a hard link number greater than 5, $0 for the entire line

ll |  Awk '$2>5 {print $0}'

Original file:

score:

| Math| English| C++| OS| SE|

| 95 | 89 | 92 | 98 | 99|

| 66 | 78 | 82 | 81 | 90|

| 79 | 65 | 77 | 82 | 35|

Command:

cat score |  Awk 'BEGIN {FS="| "; print "MY Score List"}\\

NR==1 {print $0 "Sum" FS "Average" FS}\\

NR>1 {sum=$1 +$2+$3+$4+$5;aver=sum/5;print $0 sum FS aver FS}\\

END {print "Finish"}'


Output:

MY Score List

| Math| English| C++| OS| SE| Sum| Average|

| 95 | 89 | 92 | 98| 99| 374| 74.8|

| 66 | 78 | 82 | 81| 90| 307| 61.4|

| 79 | 65 | 77 | 82| 35| 303| 60.6|

Finish

Copyright © Windows knowledge All Rights Reserved