Linux Text Processing

Source: Internet
Author: User
Tags diff log log


1. Text Processing-echo



Echo-e "\033[40;35m ..."


Background color range: 40--49
40: Black 41: Crimson 42: Green
43: Yellow 44: Blue 45: Purple
46: Dark green 47: Baise


Font Color range: 30-39
30: Black 31: Red 32: Green
33: Yellow 34: Blue 35: Purple
36: Dark green 37: Baise


eg
Echo-e "\033[40;35mhello word\033[0m"


2. Text Processing-sort


sort [Options] input file


-O Output file
-D Sort by dictionary order
-N Output by number size
-R output sorting results in reverse order
-k specifies that the classification is a subcategory of the domain
-t domain delimiter; separate fields with non-whitespace or TAB key


eg
Sort-k3-n-r-t:/etc/passwd|more


3. Text Processing-diff


diff[Options] File/directory File/directory


Diff/etc/passwd/etc/passwd.bak


-Q shows only if there are no differences, no detailed information is displayed
-C Displays all the text and marks the difference
-B does not check for differences in whitespace characters
-B does not check for blank lines
-R compare files in subdirectories


4.grep Options


-N Displays the number before each line


Application examples


A. Set case
#grep ' [tt]his ' file1.txt


B. Do not match the beginning of the line
#grep ' ^[^#] ' file.txt//Find character string not beginning with #


C. Match any character
#grep "S...N" File3.txt


s123n true
s34n false


Note: Three points three characters


D.-v


Grep-v "Hello" file




Search for rows that do not contain a "hello" string


5.sed Operation Action


S alternative operation
I insert command
A additional command
D Delete all rows that match
D Delete all rows that match


eg


#sed-n ' 1,4 p '/etc/passwd prints 1 to 4 lines of/etc/passwd


#sed '/80/d ' file.txt delete all rows that match 80


#sed ' s/var/usr/g ' file.txt all var is replaced with USR


#sed ', $s/help/man/g ' file.txt all help replaced with man


#sed the third line of the "3i\\chengyaogen" log log file is inserted before the Chengyaogen


Insert Chengyaogen after the third line of the "3a\\chengyaogen" log log file #sed


#sed "3d" log delete the third line of the log file


6.awk format


awk [Options] ' awk script ' input file


-F FS uses FS as the field delimiter for input records
-f filename reads awk_script from file filename
-V var=value set variable for Awk_script


awk built-in variables


Variable function default


FS input Field separator space or tab
RS input record delimiter line break
OFS output field delimiter space or tab
ORS output record delimiter line break
NF current record number of non-empty fields $NF last column
NR $NR The corresponding line number of the record number read from all files


Application examples




#awk-F: ' {print nr,$1,$2} '/etc/passwd
Outputs the first and second columns of the/etc/passwd and displays the corresponding line numbers


#awk-F: ' {print nr,$1, $NF} '/etc/passwd
The first column of the output/etc/passwd, the last column and the corresponding line number


#awk-F: ' Nr%10==5{print nr,$0} '/etc/passwd
An entire line of information for the output line number to 10 for the remainder of 5


#awk-F: ' Nr==8,nr==13{print nr,$0} '/etc/passwd


Output line number [8,13] of the line information


#awk-F: ' {if ($3>50) {print '} '/etc/passwd

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.