LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 704|回复: 0

sed & bash 学习手记(1)

[复制链接]
发表于 2005-10-12 18:57:33 | 显示全部楼层 |阅读模式
1. 关于 -n 的说明
The default operation of sed is to output every input line. The -n option suppresses the automatic output. When specifying this option, each instruction intended to produce output must contain a print command, p. Look at the following example.

2. debian 下读取指定软件包的所有信息(实质上是读取相邻两个空行之间的所有数据)

    sed -n '/^Package: libc6-udeb/{:next;N;/\n$/!b next;p}' Packages
   
    PS: 每 N 一行,缓冲区会加入 \n<newline> ,当读入一空行时,缓冲区的最后一个字符就是 \n

3. 读取所有软件包的名字

    sed -n 's/^Package: \(.*\)/\1/p' Packages

    PS: \1 表示保留 pattern 中第一个子模式,即匹配括号中模式的予以保留

4. 提取含有特定信息的数据块(比如获取所有 gnome 用到的包)

    sed '/^Package:/{:next;N;/\n$/!b next;/Section: gnome/b;d}' Packages

5. sed 中使用 bash 中定义的变量

    sed -n "/^Package: $file/{:next;N;/\n$/! b next;p}" Packages

    PS: 注意 sed 的命令部分是用“”括起来的,这样可以引用 bash中定义的变量 file;另外!和 b之间        要留有空格否则bash 会报错,因为在bash中!有特殊用途。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表