余苏明的幻想乡

summingyu's blog


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 公益404

  • 搜索
余苏明的幻想乡

Chapter 10. Awk Variables and Operators

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

62. Variables(变量)

关键字不能被使用为变量名。
和其他的编程语言不同,你不需要声明变量才能使用它。
如果你想初始化变量,最好在BEGIN块,它将只执行一次

实例

创建一个文件

1
2
3
4
5
6
7
$ vi employee-sal.txt
101,John Doe,CEO,10000
102,Jason Smith,IT Manager,5000
103,Raj Reddy,Sysadmin,4500
104,Anand Ram,Developer,4500
105,Jane Miller,Sales Manager,3000

阅读全文 »
余苏明的幻想乡

Chapter 9. Awk Built-in Variables(内建变量)

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

55. FS - Input Field Separator(输入字段分隔符)

默认的输入字段分隔符为空格space

  • 可以使用awk选项-F设置
  • 可以使用内建变量FS=""在BEGIN块内设置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    awk 'BEGIN { FS=","; \
    print "-------------\nName\tTitle\n-------------" } \
    { print $2,"\t",$3; } \
    END {print "-------------"}' employee.txt
    -------------
    Name Title
    -------------
    John Doe CEO
    Jason Smith IT Manager
    Raj Reddy Sysadmin
    anand Ram Developer
    Jane Miller Sales Manager
    -------------
阅读全文 »
余苏明的幻想乡

Chapter 8. Awk Syntax and Basic Commands

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

Awk是一个强大的处理文本的语言

  • AWK is original AWK.
  • NAWK is new AWK.
  • GAWK is GNU AWK.所有的Linux发行版本都自带GAWK
    在linux系统中,你可以看到/bin/awk是一个软链接,链接到/bin/gawk
    阅读全文 »
余苏明的幻想乡

Chapter 7. Sed Multi-Line Commands and loops

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

sed多行命令和循环,默认情况下sed命令是单行处理

46. Append Next Line to Pattern Space (N command)

添加下一行到P空间
1472615225743.png
将回车替换成\n写入一行

阅读全文 »
余苏明的幻想乡

Chapter 6. Sed Hold and Pattern Space Commands

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

sed的保留空间和模式空间

  • pattern space(模式空间)相当于车间sed把流内容在这里处理;
  • hold space(保留空间)相当于仓库,加工的半成品在这里临时储存(当然加工完的成品也在这里存储)。
    阅读全文 »
余苏明的幻想乡

Chapter 5. Additional Sed Commands(扩展的sed命令)

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

28. Append Line After (a command)

你可以使用a命令在匹配到行后面添加新行

Syntax(语法)

1
$ sed '[address] a the-line-to-append' input-file

Add a new record(记录)to the employee.txt file after line number:

对employee.txt文件在指定行号后面添加一个新的记录

1
2
3
4
5
6
7
$ sed '2 a 203,Jack Johnson,Engineer' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
203,Jack Johnson,Engineer
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

阅读全文 »
余苏明的幻想乡

Chapter 4. Sed Execution(执行)

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

23. Multiple Sed Commands in Command Line

在命令行上多个sed命令

1. Use multiple -e option in the command line

使用多个-e选项

1
sed -e 'command1' -e 'command2' -e 'command3'

阅读全文 »
余苏明的幻想乡

Chapter 3. Regular Expressions(正则表达式)

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

Chapter 3. Regular Expressions(正则表达式)

20. Regular Expression Fundamentals(正则表达式基础)

  1. Beginning of line (^ )
  2. End of line ( $)
  3. Single Character (.)
  4. Zero or more Occurrences (*)
  5. One or more Occurrence (\+)
  6. Zero or one Occurrence (\?)
  7. Escaping the Special Character (\)
  8. Character Class ([0-9])
    阅读全文 »
余苏明的幻想乡

Chapter2 Sed Substitute 代替 Command

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

Chapter 2. Sed Substitute(代替) Command

6. Sed Substitute Command Syntax

1
2
3
sed '[address-range|pattern-range] s/originalstring/replacement-string/[substitute-flags]' inputfile
#sed '[地址范围|模板范围] s/原始字符串/替换字符串/[替换flag]' 输入文件
  • 地址范围或者模板范围是可选的。如果你没有选择这个选项, sed将会在所有行执行替换命令
  • s – 告诉sed执行替换命令
  • original-string –原始字符串将会在输入文件中搜寻。 原始字符串也能够是正则表达式
  • replacement-string – Sed 将会用这个字符串替换原始字符串
  • substitute-flags是可选的。 它可以包含以下一个或者多个值

源文件不会被修改

阅读全文 »
余苏明的幻想乡

Chapter 1: Sed Syntax and Basic Commands

发表于 2016-11-16  |  2016-11-21   |   分类于 学习笔记 , sed and awk 101 hacks   |   临幸次数

Chapter 1: Sed Syntax and Basic Commands

sed examples

使用的sed 案例均为下面文件

1
2
3
4
5
6
$ vi employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

阅读全文 »
123
Summingyu

Summingyu

“Talk is cheap. Show me the code.”————多说无益,看代码

30 日志
9 分类
8 标签
github weibo qq
友情链接
  • quericy
  • fenge
© 2017 Summingyu
由 Hexo 强力驱动
主题 - NexT.Muse