sed
来源:互联网 发布:java调用sql存储过程 编辑:程序博客网 时间:2024/06/05 11:26
another_file:
text from another file!
t_file:
abb ccc Josephjosephlast linedate: 12-11-2011DATE:12/11/2011 1234511 --> 221110111
sed cmd:
#print out the lines matched sed -n '/[jJ]oseph/p' t_file#delete specified linessed '1,3d' t_file#print last linesed -n '$p' t_file#print the matched linessed -n '/joseph/p' t_file#print all lines, and print the matched lines twice.sed '/joseph/p' t_file#campare the result...sed 's/[jJ]oseph/& Yang/g' t_filesed -n 's/[jJ]oseph/& Yang/g' t_filesed 's/[jJ]oseph/& Yang/gp' t_filesed -n 's/[jJ]oseph/& Yang/gp' t_file#analyze to the cmd abovesed -n 's/\([jJ]oseph\)/Yang \1/gp' t_file#change the default delimitersed -n 's-1-2-gp' t_filesed -n 's/1/2/gp' t_file#print lines from '3' to '5'sed -n '/3/,/5/p' t_filesed -n '/3/,/5/s/$/###tail###/p' t_filesed -e 's/11/%%/g' -e 's/\(%%\)/##\1##/pg' -n t_filesed -e 's/11/%%/g' -e 's/\(%%\)/##\1##/p' -n t_file#file readingsed '/4/r another_file' t_file#file writingsed -n '/3/,/4/w file_write' t_file && cat file_write#append operationsed '/5/a \ ---> append one line after <-----' t_file#insert the contents before the specified linesed '/5/i \---> append one line before <-----' t_file#change the contentssed '/5/c \---> change one line <-----' t_file# 'n' cmd, sed command will applied to the matched line's next linesed '/^date/ { n; s/\/2011$/\/change next line /p; }' t_file# xhg charsed 'y/01/xy/' t_file#match the line has 'Joseph', change 'joseph' to 'hofrat'# in the matched line's next line, and sed exit sed '/Joseph/{ n; s/joseph/hofrat/; q ; }' t_file#when read one line from the file, sed place the line to mode buffer(size 8192Bytes)# and the sed command applied on this line. # There exist another buffer named holding buffer(size 8192B)# 'h' cmd will save the line in the mode buffer to the holding buffer.# 'g' cmd will excute the oppsite operation.sed -e '/DATE/ {h;d;}' -e '/11011/{g;}' t_filesed -e '/DATE/ {h;d;}' -e '/11011/{x;}' t_file # 'g' or 'x' # the cmd above, do the following operation:# the line contains 'DATE' will firstly be placed to the mode buffer and then# placed to holding buffer by 'h' cmd. The line in mode buffer will be deleted by # cmd 'd'. # then, excute the next sed cmd.# when line contain '11011' be matched, the line will be replaced by the line# we stored in the holding buffer before.#NOTE: cmd 'G' just print the line which we save in holding buffer after the matched line.sed -e '/DATE/ {h;d;}' -e '/11011/{G;}' t_file
sed script file
sed_script
#my firs sed script/DATE/ {h;d;} /11011/{G;}
How to execute the script:
#chmod +x sed_script
# sed -f sed_script t_file
- sed
- Sed
- SED
- sed
- sed
- sed
- sed
- sed
- sed
- Sed
- sed
- sed
- sed
- sed
- sed
- sed
- sed
- sed
- PHP HTML 生成 PDF
- 近期学习计划
- hdu_1165
- python3.2 一个简单的socket 程序
- 用 PHP 读取和编写 XML DOM
- sed
- php导出到Excel 或 CSV (附utf8、gbk 编码转换)
- __call、__set 和 __get的用法
- 【面试】linux下查看和添加PATH环境变量
- php 操作数组 (合并,拆分,追加,查找,删除等)
- CentOS下用命令查看IP地址
- Linux中通用链表(list)的解析
- 只要位图 MFC
- 哈希表查找不成功时的平均查找长度