<title>Mac Text Processing command sharing</title>Mac Text Processing command share table of Contents
- 1. Sed
- 1.1. Text Lookup Replacement
- 1.2. Text insertion
- 1.3. Text line deletion
- 1.4. Execute SED script
- 2. awk
- 3. Sort
- 4. Uniq
- 5. Other
1Sed
The default SED command only makes changes to the standard output and does not affect the source file, but if you add-I, the source file is changed. The-i parameter used in Mac differs from Linux. Only instructions for Mac commands are followed. The-i ' I plus the backup file name can affect the source file. The default is the same as the original file name and does not produce a backup file. The file for this test is Csophys. The contents are as follows: Cat Csophysi am the first line! Line second is me.
1.1Text Lookup substitution
"' ' s/line/line/' Csophys
1.2Text insertion
A after the target line is inserted, I is inserted before the target line.
Insert ' Insert'$line a\xxxx' file
$line This is the target row, you can also use the regular to locate all matching rows.
Insert ' Csophys
You can also insert the contents of other files directly after the matching rows, such as:
'/first/r command.org ' Csophys
1.3Text line Delete
Delete lines in text
'/se.*/d ' Csophys
1.4Execute SED script
Cat sed.rules/first/first/s/second/second/
Sed-f Sed.rule Csophysi am the first line! Line SECOND is me.
More commands can refer to man sed.
2Awk
awk ' {pattern + action} ' {filenames}
When awk is processing text on each line, the default is to separate each domain by a space and handle each domain. You can also specify the delimiter directly by adding-F after awk.
'/rw/{print ' third column: "$ $" \t|\t "" 5th column: "$ $} ' third column: Csophys | 5th column: 3154 third column: Csophys | 5th column: 42 third column: Csophys | 5th column: 5253 third column: Csophys | 5th column: 32 third column: Csophys | 5th column: 35
Ls-l | Awk' begin{ofs= ' & '; count=10;for (i=0;i<3;i++) print ' Start '}/rw/{print ++count,nr,$3,$5}end{print ' End '} 'Start start One&2&csophys& at A&3&csophys&3675 -&4&csophys& the -&5&csophys&5253 the&6&csophys& + -&7&csophys& *End
You can use printf to format the output in awk, or you can use the IF Judgment statement
Ls-l | Awk' begin{ofs= & '; count=10;for (i=0;i<3;i++) print "Start"}/rw/{if (nr>3) printf ("printf:%s,%s,%s,%s\n", + + count,nr,$3,$5)}end{print "End"} '<i++)Print"Start"}/rw/{if(Nr>3)Printf("printf:%s,%s,%s,%s\n", ++count,nr,$3,$5)}END{Print "End"}'StartStartStartprintf:11,4,csophys,42printf:12,5,csophys,5253printf:13,6,csophys,32printf:14,7,csophys,35End
The meaning of the awk internal variable name is as follows:
Variable name meaning ARGC command line argument number ARGV command line argument array filename current input file name FNR The record number in the current file FS input field delimiter, default is a space RS Enter record delimiter NF current record field number NR so far record number OFS output field delimiter ORS output record delimiter
A more powerful feature reference man awk. or refer to the blog:
- http://www.zsythink.net/archives/tag/awk/
- Https://www.tutorialspoint.com/awk/awk_quick_guide.htm
3Sort
Sort [-NTKR] file name #-n numeric sort #-t Specify delimiter #-k specify column #-r reverse sort
'/rw/{print $ ' | Sort-n-R 52531608
Ls-l | Sort-k5-n-r-rw-r--r--1Csophys Staff5253 9 3 11:25passwd-rw-r--r--1Csophys Staff1817 9 3 22:29command.org-rw-r--r--1Csophys Staff the 9 1 23:14csophys-rw-r--r--1Csophys Staff * 9 3 11:37test-rw-r--r--1Csophys Staff + 9 2 22:59Sed.ruletotal -
Ls-l | Sort-k2-T":"Total --rw-r--r--1Csophys Staff the 9 1 23:14csophys-rw-r--r--1Csophys Staff5253 9 3 11:25passwd-rw-r--r--1Csophys Staff2157 9 3 22:31command.org-rw-r--r--1Csophys Staff * 9 3 11:37test-rw-r--r--1Csophys Staff + 9 2 22:59Sed.rule
4Uniq
Uniq generally need to be used in conjunction with sort. Uniq's job is to delete the same rows consecutively, so it works well after sort. The-c parameter can print out the number of repetitions.
'/rw/{print $} ' '/rw/{print $} ' | Uniq-c5 Csophys
5Other
- Cut Cut can intercept a specific column or columns separated by the specified delimiter.
- Tr. The main purpose of the TR command is to convert or delete text
- Paste The Paste function is to merge rows according to the specified delimiter
- Split Split can be used to achieve the segmentation of the file, in line with the number of split and split by size of the two modes.
Author: Chensheng
created:2017-09-04 Mon 23:16
Validate
Mac Text Processing command sharing