Linux Text Processing

Source: Internet
Author: User

? Cat Viewing files
0 There is no visible content: Cat-a file
0 line number: Cat-n file blank line plus number
0 line number: Cat-b file blank line is not numbered
0 compressing adjacent empty lines: Cat-ns file
0 Inverted display: TAC file

? More/less
0 Pages: spaces, Pgdn/up
? Head/tail:
0 Default First 10 rows
0head-n 3 File first 3 rows
0tail-n 3 file after 3 lines
0 trace log: tail-f file (F-follow)
? Cut
0 Vertical cut Section column:
§cut-d:-F1,3/ETC/PASSWD (the delimiter is a colon, take the first and third fields)
§cut-d:-f1,3-5/etc/passwd
§df-h | Cut-c34-36
§ Get IP Address:
-centos7:ifconfig | Head-n 2 | Tail-n-1 | Cut-dt-f2 | Cut-d ""-f1
-centos6:ifconfig | Head-n 2 | Tail-1 | Cut-d:-f2 | Cut-d ""-f1
-ifconfig | Head-n 2 | Tail-n-1 | Tr-s "" | Cut-d ""-f2

? Suid 4: Permissions for inherited program owners
? Sgid 2: Inherit the permissions of all groups of the program; Inherit the owning group of the parent directory
? Sticky 1: You can only delete your own files

? Partition utilization:
0DF | Tr-s '% ' | cut-d "%"-f5 compress spaces, replace spaces with percent sign, then split

? Paste merge two files with row number column to one line
0paste file1 file2 Horizontal Merge
0cat file1 file2 Vertical Merge
0paste-d ":" File1 file2 plus delimiter
0paste-s file1 file2 All rows merged into one line

? Wc-word Count: Statistics line, Word, byte, file name
0wc-l file row
0WC-W file Word
0wc-c file byte number
0wc-m file character number
0wc-l file Longest Line
0 How many files are there: LS | Wc-l
0 How many users: CAT/ETC/PASSWD | Wc-l


? Httpd:web services, providing Web pages
0 Start: Service httpd start
0 Address: IP A
0 Port number: ss-ntl
0 Firewall: IPTABLES-VNL
0 shutting down the firewall: service iptables stop
0 Disable Firewall boot: chkconfig iptalbes off

? Sort
0sort file: Sort by character
0sort-t ":"-k3-n-r/etc/passwd Colon split, third column, numeric sort, reverse
0cut-d:-f3/etc/passwd | Sort-nr > Uid.txt
0 Delete duplicate: sort-u file

? Generating files
0echo {1..55}
0echo {1..55} | Tr ' \ n ' one per line
0echo {1..55} | Tr ' \ n ' | Sort-r | Head-n 1 Random Draw
0SEQ 55 | Tr ' \ n ' | Sort-r | Head-n 1


? Simulate massive user access
? Simulation: Ab-c 100-n-http://192.168.10.6/index.html
? Access log: Cat/var/log/httpd/access_log The first line is the customer IP
? Visitor (PV): Wc-l/var/log/httpd/access_log
? Which address has the highest number of accesses (whether it is an attack):
0cut-d '-F 1 access_log | Uniq-c Show frequency of occurrence
0 Top three:
§cut-d '-F 1 access_log | uniq-c | Sort-nr | Tr-s ' | Cut-d '-F 3 | Head-n 3


? Last User logon situation
0 Number of logons per account: Last | Sort | Cut-d '-F 1 | Uniq-c


? Uniq
0 Display Duplicates: Sort file | Uniq-d
0 Show frequency of occurrence: Sort File | Uniq-c
0 find two files to peer: Cat file1 file2 | Sort | Uniq-d
0 find two files out of the walk: Cat file1 file2 | Sort | Uniq-u


? Random number:
0echo &[random%55+1]

Text Processing Three musketeers:
? grep: Text filtering
? Sed: text filtering, modifying files
? awk: Support loops, branching, report printing

? grep root/etc/passwd contains the root row
? grep ' WhoAmI '/etc/passwd Current user name information
? grep $USER/etc/passwd Current user name information
? Grep-v root/etc/passwd does not contain root information
? Grep-n ROOT/ETC/PASSWD Display line number
? Grep-c root/etc/passwd, there are a few lines.
? Grep-q root/etc/passwd silent output, with $? (save silent results) using
? /dev/null Trash can not be deleted
? Grep-an3 ROOT/ETC/PASSWD After
? Grep-bn3 ROOT/ETC/PASSWD before
? Grep-cn3 ROOT/ETC/PASSWD context before and after all to
? grep root/etc/passwd | grep Wang and relationship
? Grep-e root-e wang/etc/passwd or relationship
? Grep-w root/etc/passwd contains the word root
? Nmap Scan Tool
0 Scan all boot devices: nmap-v-sp 172.16.1010.0/24
0 filter up machine: nmap-v-sp 172.16.1010.0/24 | GREP-BL Up | grep scan


? Regular Expressions:
0 Basic Regular Expressions: BRE
0 Extended Regular Expression: ERE
0GREP supports basic regular expressions by default,-E supports extended regular expressions
0 Regular expression engine: PCRE (Perl Compatible Regular Expressions)
0 matches: Character match, number of matches, position anchor, group
0 Character matches:
. Match any single character
§grep R.. t/etc/passwd
[] matches any single character within the specified range
-grep [XYZ]/etc/passwd-contains x or Y or Z
-grep [X-z]/etc/passwd-contains x to Z any character (case sensitive)
[^] matches any single character outside the specified range-

[: Alnum:] Letters and numbers
[: Alpha:] represents any English uppercase and lowercase characters, i.e. A-Z, A-Z
[: Lower:] lowercase letter [: Upper:] Uppercase
[: Blank:] white space characters (spaces and tabs)
[: Space:] Horizontal and vertical white space characters (more than [: blank:] contains a wide range)
[: Cntrl:] non-printable control characters (backspace, delete, alarm ...) )
[:d igit:] decimal digits [: xdigit:] hexadecimal digits
[: Graph:] printable non-whitespace characters
[:p rint:] printable characters
[:p UNCT:] Punctuation

* match the preceding character any time, including 0 times
-the characters in front of the asterisk appear any number of times
-grep go*gle testfile letter O appears several times
Greedy mode: Match as long as possible
. * Any character of any length
-
\? Match its preceding character 0 or 1 times
-grep go\?gle testfile letter O appears once or 0 times
\+ matches the characters in front of it at least 1 times

\{n\} matches the preceding character n times
\{m,n\} matches the preceding character at least m times, up to N times
-grep go\{2,7\}gle testfile letter O appears 2-7 Times
\{,n\} matches the preceding character up to n times
\{n,\} matches the preceding character at least n times

0 Get IP address: ifconfig | grep--color-wo "[0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\}\. [0-9]\{1,3\} "
0 Get IP address: ifconfig | grep--color-wo "\ ([0-9]\{1,3\}\.\) \{3\}[0-9]\{1,3\}"
0 Get 2018-01-12 03:48:46: (tr replace command)
-Ffmpeg-i "D:\Videos\20180112_114837.mp4" 2>&1 | Grep-wo "[0-9]\{4\}[-][0-9]\{2\}[-][0-9]\{2\}[t][0-9]\{2\}[:][0-9]\{2\}[:][0-9]\{2\}" |tr "T" "" | Uniq
0 Get 00:00:08:
-Ffmpeg-i "D:\Videos\20180112_114837.mp4" 2>&1 | grep Duration | Grep-wo "[0-9]\{2\}[:][0-9]\{2\}[:][0-9]\{2\}"
0 Get 2018-01-12
-Ffmpeg-i "D:\Videos\20180112_114837.mp4" 2>&1 | Grep-wo "[0-9]\{4\}[-][0-9]\{2\}[-][0-9]\{2\}" | Uniq

? Line starting with root: grep "^root"/etc/passwd
? Line ending with Nologin: grep "nologin$"/etc/passwd
? Blank line: grep "^$" file
? Blank lines are not displayed: Grep-v "^$" file
? Comment lines are not displayed: Grep-v "^#"/etc/fstab | Grep-v "^$"
? Displays the line containing the specified word: grep "\<root\>"/ect/passwd
? One or more spaces start with: grep "^[[:space:]]\+linux16"/boot/grub2/grub.cfg | Grep-v Rescue

? Group: Root This word appears more than two times: Echo rootrootroot | grep "\ (root\) \{2,\}"
? Call after grouping: root user begins and subsequent root appears
-grep "^\ (Root) \.*\1.*"/etc/passwd
-grep "\ (r. t\). *\1 "File (search alternative, useful)
? Get partition utilization: (Start alarm if more than 80% utilization)
0df-h | grep "^[/DEV/SDA" | Grep-o "[0,9]\{1,3\}%$" | Grep-o "[0,9]\{1,3\}"
0df-h | grep "^/DEV/SDA" | Grep-o "[[:d igit:]]\+%] | Grep-o "[[:d igit:]]\+]


? A or B begins with: Echo AXX BXX | Grep-o "\ (a\|b\) XX"

? The above is the basic regular expression, more trouble, total plus \; extended regular expressions remove \

? Extended Regular:

0 Get IP address: ifconfig | Grep-ewo "([0-9][1,3]\.) {3} [0-9] {1,3} "
0 getting the file in the system image requires the system:
-LS *.rpm | Grep-eo "\. (. *) \.rpm "| Cut-d "."-F 2 | Uqic-c
-LS *.rpm | Grep-eo "\.\<[[:alnum:]_]\+\>\.rpm$" | Cut-d "."-F 2 | Sort | Uqic-c
-LS *.rpm | Grep-eo "\. [^.] +\.rpm$ "| Cut-d "."-F 2 | Sort | Uqic-c


Linux Text Processing

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.