Linux echo, sort, sed are some of the most common commands for a beginner Linux shell script. Basically, if you can master these commands, we can write some good Linux scripts. The following are some of the applications I have encountered in the following commonly used Echo,sort, SED, and other commands;
1. ECHO-E: bw= "f25\nf50\nf75\nf100\n";
If the echo $BW directly, the result is f25\nf50\nf75\nf100\n. So we have to add the-e option to achieve the purpose of line wrapping, that is, Echo-e $BW
note:echo-e handling Special charactersIf the following character appears in the string, it is handled in particular, not as a general text output: \a emits a warning, \b deletes the previous character, \c ends with a newline symbol, \f wraps but remains in its original position; \ n Wraps and the cursor moves to the beginning of the line; \ r The cursor moves to the beginning of the line but does not wrap; Insert Tab;\v Same as \f, \ \ insert \ character, \nnn insert ASCII character represented by NNN (octal);
2. Sort order: bw= ' echo-e $BW |sed ' s/f/f/g ' |sort-ugk2|sed ' s///g ';
Sed ' s/f/f/g ': artificially creating a delimiter "" Come out
Sort-u: Clear Duplicate rows
-G: Sort by number size: Generally speaking, if there are five numbers: 1 3 11 12 21, it will be ranked by the size of the first number, that is, 1 11 12 21 3, plus-G or-n will be sorted normally, i.e. 1 3 11 12 21
-K: Commonly used with-T,-T is a delimiter, the default is a space, SORT-UGK2 is actually sort-u-T ""-k2-g. Where-k2 refers to sorting by the size of the value following the separator
Sed ' s///g ': Remove delimiter
Note:sort [-bcfmnrtk][source file][-o output file]
Additional notes: Sort can be sorted for the content of the text file, in the unit of behavior.
Parameters
-B ignores whitespace characters that begin before each line.
-C checks whether the files are sorted in order.
-U Remove Duplicate rows
-f sort, ignores uppercase and lowercase letters.
-M sorts the first 3 letters according to the abbreviation of the month.
-N Sorts by the size of the numbers. Compare according to string numerical value
-G compare according to general numerical value
-o< the output file > The sorted result into the specified file.
-R is sorted in reverse order.
-R Random Sort
-t< Delimited character > specifies the field separator character to use when sorting.
-K selects which interval to sort.
Reference: http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html
3. Sed:cat Runlist.txt | Sed ' s/^#.*//g ' |sort-r;
If the content inside the Runlist.txt is:
Test_short/test_ul_cellid
Test_short/test#_ul_dl_cfg. 2
Test_short/test_ul_mcs
#test_short/test_ul_rnti in the ADB list
Cat Runlist.txt | sed ' s/^#.*//g ' |sort-r: Then this is a line that begins with # to be deleted.
Test_short/test_ul_cellid
Test_short/test#_ul_dl_cfg. 2
Test_short/test_ul_mcs
Cat Runlist.txt | sed ' s/#.*//g ' |sort-r: Then this sentence is to remove the content after the #.
Test_short/test_ul_cellid
Test_short/test_ul_mcs
test_short/test#
Cat Runist.txt | Sed '/#/d ': Delete the line that appears
Test_short/test_ul_cellid
Test_short/test_ul_mcs
3a. Match the data and then do the operation
Just add a regular match on the basis above
Sed "/matching mode/method of processing" file.txt
Sed "/^root/d" file.txt to start with root delete
Cat Runist.txt | Sed '/#/s/test_short/bugs/g/': Find the line that appears and replace it with Bugs Test_short
3b. A more interesting example: How to replace \ nthe all rows into one line
We can use TR instead of SED to do this thing: tr "\ n" "": Replace \ n with a space
note:sed [-NEFR] [action] File
Parameters:
-N Quiet mode, when the SED processing, all data from stdin will be output to the terminal, plus-n will only output which line of processing
-E performs SED action editing directly on the command column
-F Write the SED action directly in the file
-R SED action supports extended regular expression (default is only the underlying regular)
-I directly modify the contents of the file (use caution, especially when using system files to do exercises) is very useful, do not add this option, will only print out on the screen
Action:
A append: Increment, increment on the next line of the current row
C: Replace, replace the line between N1 and N2
D Delete: Delete
I insert, the previous line of the current row is inserted
P printing, often with-n use
s replace, s/old/new/g
http://blog.csdn.net/hello_hwc/article/details/40118129
Summary of some commands for Linux echo, sort, sed, etc.