标签:style os 文件 io for cti ar line
| 1. basic options | |||||||||||||||||||
| sed [options] {command} {input-file} | |||||||||||||||||||
| sed -n | sed -n ‘p‘ a.out | 打印a.out文件内容 | |||||||||||||||||
| -n: 取消默认打印到屏幕功能 | |||||||||||||||||||
| sed -f | |||||||||||||||||||
| sed [options] -f {sed-commands-in-file} {input-file} | |||||||||||||||||||
| test-script.sed | /^root/ p | ||||||||||||||||||
| /^nobody/ p | |||||||||||||||||||
| sed -n -f test-script.sed /etc/passwd | 执行test-script.sed中的sed语句 | ||||||||||||||||||
| sed -e | sed [options] -e {sed-command-1} -e {sed-command-2} {input-file} | ||||||||||||||||||
| sed -n -e ‘/^root/ p‘ -e ‘/^nobody/ p‘ /etc/passwd | |||||||||||||||||||
| sed -n \ -e ‘/^root/ p‘ \ -e ‘/^nobody/ p‘ \ /etc/passwd |
逐条执行sed语句(而不必把语句卸载文件中,adhoc分析用) | ||||||||||||||||||
| sed ‘{}‘ | |||||||||||||||||||
| sed [options] ‘{ | sed-command-1 | ||||||||||||||||||
| sed-command-2 | |||||||||||||||||||
| }‘ {input-file} | |||||||||||||||||||
| sed -n ‘{ | 逐条执行sed语句(而不必把语句卸载文件中,adhoc分析用) | ||||||||||||||||||
| /^root/ p | |||||||||||||||||||
| /^nobody/ p | |||||||||||||||||||
| }‘ /etc/passwd | |||||||||||||||||||
| sed scripting flow | |||||||||||||||||||
| read | |||||||||||||||||||
| excute | |||||||||||||||||||
| repeat | |||||||||||||||||||
|
|
|||||||||||||||||||
| sed ‘p‘ | sed ‘p‘ employee.txt | 屏幕上打印每条语句两次 | |||||||||||||||||
| sed -n ‘p‘ employee.txt | 屏幕上打印每条语句一次, 相当于cat | ||||||||||||||||||
| sed range(selective lines to execute) | |||||||||||||||||||
| range by comma | sed -n ‘2 p‘ employee.txt | 只打印第二行 | |||||||||||||||||
| sed -n ‘1,4 p‘ employee.txt | 打印第一至四行 | ||||||||||||||||||
| sed -n ‘2,$ p‘ employee.txt | 打印第二行到最后一行 | ||||||||||||||||||
| range by plus | sed -n ‘1,+2 p‘ employee.txt | 从第一行开始,再打印紧接着的两行 | |||||||||||||||||
| n,+m | |||||||||||||||||||
| range by tilde | n~m | 从第n行开始,每个m行打印 | |||||||||||||||||
| n, n+m, n+2m,n+3m,… | |||||||||||||||||||
| sed -n ‘1~2p‘ employee.txt | 打印奇数行的内容 | ||||||||||||||||||
| Pattern matching | |||||||||||||||||||
| sed -n ‘/Jane/ p‘ employee.txt | 输出包含Jane的行 | ||||||||||||||||||
| sed -n ‘/Jane/,4 p‘ employee.txt | |||||||||||||||||||
| 输出从第一个包含Jane的行开始,到第四行结束 | |||||||||||||||||||
| 如果1到4行没有包含Jane 的行,则输出后面包含Jane的行 | |||||||||||||||||||
| sed -n ‘/Raj/,$ p‘ a.txt | 输出从第一个包含Raj的行开始,到最后一行 | ||||||||||||||||||
| sed -n ‘/Raj/,/Jane/ p‘ a.txt | 输出从第一个包含Raj的行开始,到紧接的包含Jane的行结束 | ||||||||||||||||||
| sed -n ‘/Raj/,+2p‘ a.txt | 输出从第一个包含Raj的行,及紧接着的两行 | ||||||||||||||||||
| sed ‘d‘ | patten match 的逆运算 | ||||||||||||||||||
| sed ‘2d‘ a.txt | 删除第二行;结果输出出第二行以外的所有行 | ||||||||||||||||||
| sed ‘2,$ d‘ a.txt | 删除第二行至最后一行; 输出剩下的行 | ||||||||||||||||||
| sed ‘/Manager/ d‘ a.txt | 删除包含Manager的行;输出剩下的行 | ||||||||||||||||||
| sed ‘/Jason/,4 d‘ a.txt | 删除第一个包含Jason的行至第四行 | ||||||||||||||||||
| 如果前四行中没有包含Jason的行,则将删除剩下的行 | |||||||||||||||||||
| sed ‘/Raj/,$ d‘ a.txt | 删除从第一个包含Raj的行至最后一行 | ||||||||||||||||||
| sed ‘/Raj/, /Jane/, d‘ a.txt | 删除包含Raj的行至后面第一个包含Jane的行 | ||||||||||||||||||
| sed ‘/Jason/,+2d‘ a.txt | 删除从包含Jason的行及紧接着的两行 | ||||||||||||||||||
| sed ‘/^$/ d‘ a.txt | 删除所有空行 | ||||||||||||||||||
| sed ‘/^#/d‘ a.txt | 删除所有注释行 | ||||||||||||||||||
| sed ‘w‘ | |||||||||||||||||||
| sed ‘w out.txt‘ a.txt | 把a.txt内容写入out.txt | ||||||||||||||||||
| sed -n ‘w out.txt‘ a.txt | 直接写入,取消将内容打印到屏幕 | ||||||||||||||||||
| sed -n ‘1,4 w out.txt‘ a.txt | 将一至四行,写入到out.txt 文件 | ||||||||||||||||||
| sed -n ‘2,$ w out.txt‘ a.txt | 2至最后一行 写入 | ||||||||||||||||||
| sed -n ‘1~2 w out.txt‘ a.txt | 1,3,5,7,..行写入 | ||||||||||||||||||
| sed -n ‘/Jane/ w out.txt‘ a.txt | == | sed -n ‘/Jane/ p‘ a.txt > out.txt | |||||||||||||||||
| sed -n ‘Jason,4 w out.txt‘ a.txt | |||||||||||||||||||
| …. | |||||||||||||||||||
| sed substitution command | |||||||||||||||||||
| sed ‘[address-range|pattern-range] s/orginal-string/replacement-string/[substitute-flags]‘ input-file | |||||||||||||||||||
| sed ‘s/Manager/Director/‘ a.txt | 用Director替换所有的Manager | ||||||||||||||||||
| sed ‘/Sales/s/Manager/Director/‘ a.txt | |||||||||||||||||||
| 只对包含Sales的行进行替换 | |||||||||||||||||||
| g flag | 默认情况下,替换只替换每一行中第一次出现的pattern,而不是一行中所有的pattern | ||||||||||||||||||
| g可以替换所有的pattern | |||||||||||||||||||
| sed ‘s/a/A/g‘ a.txt | 将所有的小写字母a替换为大写字母A | ||||||||||||||||||
| number flag | |||||||||||||||||||
| sed ‘s/a/A/2‘ a.txt | 每行的第二个小写字母a替换为大写字母A | ||||||||||||||||||
| print flag | sed -n ‘s/John/Johnny/p‘ a.txt | 打印所有被替换后的行 | |||||||||||||||||
| w write flag | sed -n ‘s/John/Johnny/w output.txt‘ a.txt | ||||||||||||||||||
| i ignore case flag | |||||||||||||||||||
| sed ‘s/john/Johnny/i‘ a.txt | |||||||||||||||||||
| e excute flag | execute as shell command | ||||||||||||||||||
| file.txt | |||||||||||||||||||
| /etc/passwd | |||||||||||||||||||
| /etc/group | |||||||||||||||||||
| sed ‘s/^/ls -l /e‘ file.txt | |||||||||||||||||||
| == | |||||||||||||||||||
| 执行 | ls -l /etc/passwd | ||||||||||||||||||
| ls -l /etc/group | |||||||||||||||||||
| sed substition delimeter | |||||||||||||||||||
| sed ‘s/…/…/‘ a.txt | |||||||||||||||||||
| sed ‘s^…^…^‘ a.txt | |||||||||||||||||||
| sed ‘s@…@…@‘ a.txt | |||||||||||||||||||
| sed ‘s!…!…!‘ a.txt | |||||||||||||||||||
| multiple sustitute command affecting the same line | |||||||||||||||||||
| list of command | |||||||||||||||||||
| command-1 on the pattern space | |||||||||||||||||||
| command-2 on the newly changed pattern by command-1 | |||||||||||||||||||
| …. | |||||||||||||||||||
| sed ‘{ | |||||||||||||||||||
| s/Developer/IT Manager/ | |||||||||||||||||||
| s/Manager/Director/ | |||||||||||||||||||
| }‘ | |||||||||||||||||||
| a.txt | |||||||||||||||||||
| & get matched pattern | |||||||||||||||||||
| sed ‘s/^[0-9][0-9][0-9]/[&]/g a.txt | |||||||||||||||||||
| 101,… | [101],.. | ||||||||||||||||||
| 102,.. | [102],… | ||||||||||||||||||
| sed ‘s/^.*/<&>/‘ a.txt | 所有的行,前加<后加> | ||||||||||||||||||
| \1, \2,..single group | |||||||||||||||||||
| sed ‘s/\([^,]*\).*/\1/g‘ a.txt | |||||||||||||||||||
| \([^,]*\) | 匹配到第一个非,的字符 | ||||||||||||||||||
| \1 | 用第一个匹配进行替换 | ||||||||||||||||||
| sed ‘s/\([^:]\)/\1/g‘ /etc/passwd | 匹配第一个field | ||||||||||||||||||
| echo "The Geek Stuff" | sed ‘s/\(\b[A-Z]\)/\(\1\)/g‘ | |||||||||||||||||||
| (T)he (G)eek (S)tuff | |||||||||||||||||||
| Chapter 3 | |||||||||||||||||||
| Regular Expression basic | |||||||||||||||||||
| ^ | beginning of line | ||||||||||||||||||
| $ | end of line | ||||||||||||||||||
| . | single character | ||||||||||||||||||
| * | zero or more occurences | ||||||||||||||||||
| \+ | one or more occurences | ||||||||||||||||||
| \? | zero or one occurences | ||||||||||||||||||
| [0-9] | character class | ||||||||||||||||||
| | | or operation | sed -n ‘/101\|102/p‘ a.txt | |||||||||||||||||
| {m} | exactly m occurrences | \{m\} | |||||||||||||||||
| {m,n} | m to n occurrences | ||||||||||||||||||
| \b | word boundary | ||||||||||||||||||
| \n | back reference | ||||||||||||||||||
| sed -e ‘s/#.*//; /^$/ d‘ employee.txt | |||||||||||||||||||
| conver Dos file format to Unix file format | |||||||||||||||||||
| sed ‘s/.$//‘ filename | |||||||||||||||||||
| Chapter 4 | |||||||||||||||||||
| Sed execution | |||||||||||||||||||
| #sed comments begin with # | |||||||||||||||||||
| to find the sed interpreter, sed file begins with this line | |||||||||||||||||||
| #!/bin/sed -f | |||||||||||||||||||
| chmod u+x script.sed | |||||||||||||||||||
| ./script.sed input.txt | |||||||||||||||||||
| first line | |||||||||||||||||||
| #!/bin/sed -nf | suppress the output | ||||||||||||||||||
| ‘-i modifye the input file | |||||||||||||||||||
| sed -i ‘s/John/Johnny/‘ a.txt | replace and modify a.txt | ||||||||||||||||||
| sed -ibak ‘s/John/Johnny/‘ a.txt | replace and modify a.txt | ||||||||||||||||||
| also save a copy of a.txt as a.txtbak | |||||||||||||||||||
| Chapter 5 Addition Sed Commands | |||||||||||||||||||
| a append line | |||||||||||||||||||
| sed ‘[address] a the-line-to-append‘ input-file | |||||||||||||||||||
| sed ‘2 a 203,Jack Johnson,Engineer‘ employee.txt | 第二行后添加 | ||||||||||||||||||
| sed ‘$ a 106,Jack Johnson,Engineer‘ employee.txt | 最后一行后添加 | ||||||||||||||||||
| sed ‘/Jason/a\ | |||||||||||||||||||
| 203,Jack Johnson,Engineer\ | |||||||||||||||||||
| 204,Mark Smith,Sales Engineer‘ employee.txt | 有Jason的行后添加两行 | ||||||||||||||||||
| i-command insert before | |||||||||||||||||||
| sed ‘[address] i the-line-to-append‘ input-file | |||||||||||||||||||
| c-command change line, replace it with the specified | |||||||||||||||||||
| sed ‘[address] c the-line-to-append‘ input-file | |||||||||||||||||||
| sed ‘/Jason/ { | |||||||||||||||||||
| a\ | |||||||||||||||||||
| 204,Jack Johnson,Engineer | |||||||||||||||||||
| i\ | |||||||||||||||||||
| 202,Mark Smith,Sales Engineer | |||||||||||||||||||
| c\ | |||||||||||||||||||
| 203,Joe Mason,Sysadmin | |||||||||||||||||||
| }‘ employee.txt | |||||||||||||||||||
| l: print hidden characters | |||||||||||||||||||
| sed -n l a.txt | |||||||||||||||||||
| =: show print line number | |||||||||||||||||||
| sed = a.txt | |||||||||||||||||||
| sed ‘/Jane/ =‘ a.txt | |||||||||||||||||||
| sed -n ‘/Raj/ =‘ a.txt | just show the number of the line containig Raj | ||||||||||||||||||
| sed -n ‘$ =‘ a.txt | show the total number of lines | ||||||||||||||||||
| y: transform operation | |||||||||||||||||||
| sed ‘y/abcde/ABCDE/‘ a.txt | 字母对应替换: a-A, b-B, c-C, d-D, e-E | ||||||||||||||||||
| multiple inputs in command line | |||||||||||||||||||
| sed -n ‘/root/p‘ /etc/passwd /etc/group | |||||||||||||||||||
| q: quit command | |||||||||||||||||||
| r: read from file to print | |||||||||||||||||||
| sed ‘$ r log.txt‘ a.txt | print a.txt & log.txt | ||||||||||||||||||
| sed ‘/Raj/ r log.txt‘ a.txt | print the first line in a.txt containing Raj | ||||||||||||||||||
| and then print log.txt | |||||||||||||||||||
| simulating unix command | |||||||||||||||||||
| grep -v Jane employee.txt | |||||||||||||||||||
| sed -n ‘/Jane/ !p‘ employee.txt | |||||||||||||||||||
| options summary | |||||||||||||||||||
| -n | suprress the default printing | ||||||||||||||||||
| -f | sed command in file | ||||||||||||||||||
| -e | execute sed command | ||||||||||||||||||
| -i | change the original file | ||||||||||||||||||
| -l | sepecify the line length | ||||||||||||||||||
| -l 20 | |||||||||||||||||||
| or ‘l 20‘ | |||||||||||||||||||
| n: print current pattern space and fetchs the next line fro mthe input-file | |||||||||||||||||||
| Chapter 6 Sed Hold and Pattern Space Commands | |||||||||||||||||||
| Pattern Space | the internal sed buffer where sed places, and modifies, the line it reas from the input file | ||||||||||||||||||
| Hold Space | additional buffer where sed hold temporary data | ||||||||||||||||||
| support move data from between Pattern Space and Hold Space | |||||||||||||||||||
| but cannot execute sed command on hold space | |||||||||||||||||||
| Contents is retained from one cycle to the next, not deleted between cycles | |||||||||||||||||||
| x: | swap pattern space with hold space | ||||||||||||||||||
| better for the two consecutive lines with one case infor | |||||||||||||||||||
| John Doe | |||||||||||||||||||
| CEO | |||||||||||||||||||
| Jason Smith | |||||||||||||||||||
| IT Manager | |||||||||||||||||||
| Raj Reddy | |||||||||||||||||||
| Sysadmin | |||||||||||||||||||
| sed -n -e ‘{x;n}‘ -e ‘/Manager/ {x;p}‘ a.txt | |||||||||||||||||||
| h | copy pattern space to hold space | ||||||||||||||||||
| sed -n -e ‘/Manager/ !h‘ -e ‘/Manager/{x;p}‘ a.txt | |||||||||||||||||||
| /Manager/!h | if line doesn‘t contain Manager, then copy | ||||||||||||||||||
| H | append pattern space to hold space | ||||||||||||||||||
| sed -n -e ‘/Manager/!h‘ -e ‘/Manager/{H;x;p}‘ a.txt | |||||||||||||||||||
| after appending, it will be line2\nline1 with in two lines | |||||||||||||||||||
| sed -n -e ‘/Manager/!h‘ -e ‘/Manager/{H;x;s/\n/:/;p}‘ a.txt | |||||||||||||||||||
| g | copy hold space to pattern space | ||||||||||||||||||
| sed -n -e ‘/Manager/!h‘ -e ‘/Manager/{g;p}‘ | |||||||||||||||||||
| G | append hold space to pattern space | ||||||||||||||||||
| sed -n -e ‘/Manager/!h‘ -e ‘/Manager/{x;G;s/\n/:/;p}‘ empnametitle.txt | |||||||||||||||||||
| Chapter 7 Sed Multi-Line Commands and Loops | |||||||||||||||||||
| N | append next line to pattern space | ||||||||||||||||||
| sed -e ‘{N; s/\n/:/}‘ a.txt | |||||||||||||||||||
| sed -e ‘=‘ employee.txt | sed -e ‘{N;s/\n/ /}‘ | print line numbers | ||||||||||||||||||
| D | ??????????? | ||||||||||||||||||
| loop and branch | |||||||||||||||||||
| :label | defines the label | ||||||||||||||||||
| b label | branches the execution flow to the label | ||||||||||||||||||
| #!/bin/sed -nf | |||||||||||||||||||
| h;n;H;x | |||||||||||||||||||
| s/\n/:/ | |||||||||||||||||||
| /Manager/!b end | |||||||||||||||||||
| s/^/*/ | |||||||||||||||||||
| :end | |||||||||||||||||||
| p | |||||||||||||||||||
| t | ?????????????????/// | ||||||||||||||||||
标签:style os 文件 io for cti ar line
原文地址:http://www.cnblogs.com/jsquare/p/3871524.html