grep与sed配合使用一例
2013-01-15 10:32
351 查看
有时候我们需要知道搜索的内容x位于文本文件的哪一行,然后需要查看这一行附近的内容,典型的例子有:
- 查看kernel的dmesg信息时
- 阅读函数实现代码时
可以简单的通过两个命令行解决,举例如下:
$grep -n 'BUG' snow_dmesg.log
10:<4>[ 0.000000] BUG: mapping for 0x00b00000 at 0xc0900000 overlaps vmalloc space
664:<2>[ 23.566966] kernel BUG at /work2/ICS_8x25/kernel/fs/ext4/extents.c:1996!
665:<0>[ 23.573651] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
这里我搜索一个dmesg的log文件,里面有一个kernel BUG信息,通过grep命令我知道了要找的信息位于664行附近。
然后我希望查看这附件的所有内容,此时可以使用sed命令达到目的:
$sed -n '650,700p' snow_dmesg.log
<3>[ 16.800376] init: starting 'atfwd_log'
<3>[ 16.800983] init: starting 'atfwd'
<3>[ 21.821501] init: starting 'atfwd_log'
<3>[ 21.822424] init: starting 'atfwd'
<2>[ 23.529703] EXT4-fs error (device mmcblk0p13): ext4_valid_block_bitmap:282: comm er.ServerThread: Invalid block bitmap - block_group = 10, block = 327681
<3>[ 23.530108] Aborting journal on device mmcblk0p13-8.
<2>[ 23.531563] EXT4-fs (mmcblk0p13): Remounting filesystem read-only
<2>[ 23.531753] EXT4-fs error (device mmcblk0p13) in ext4_free_blocks:4643: Journal has aborted
<2>[ 23.533381] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<2>[ 23.535013] EXT4-fs error (device mmcblk0p13) in ext4_ext_remove_space:2669: Journal has aborted
<2>[ 23.537513] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<2>[ 23.546488] EXT4-fs error (device mmcblk0p13) in ext4_orphan_del:2120: Journal has aborted
<2>[ 23.554731] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<0>[ 23.564124] ------------[ cut here ]------------
<2>[ 23.566966] kernel BUG at /work2/ICS_8x25/kernel/fs/ext4/extents.c:1996!
<0>[ 23.573651] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
<4>[ 23.579118] Modules linked in: sm_event_driver sm_event_log
<4>[ 23.584676] CPU: 0 Tainted: G W (3.0.21-perf #1)
<4>[ 23.590413] PC is at ext4_ext_put_in_cache+0x14/0x48
<4>[ 23.595353] LR is at ext4_ext_map_blocks+0x2e0/0x14bc
<4>[ 23.600386] pc : [<c01ff260>] lr : [<c0202a08>] psr: 60000113
<4>[ 23.600391] sp : df647a10 ip : 00000000 fp : df232254
<4>[ 23.611848] r10: 00000000 r9 : 00000000 r8 : 00000000
<4>[ 23.617053] r7 : 00000000 r6 : df71ac20 r5 : df647b80 r4 : 00000000
<4>[ 23.623561] r3 : 00000000 r2 : 00000000 r1 : 00000000 r0 : df2322d0
OK,我已看到到希望查看的内容了。
- 查看kernel的dmesg信息时
- 阅读函数实现代码时
可以简单的通过两个命令行解决,举例如下:
$grep -n 'BUG' snow_dmesg.log
10:<4>[ 0.000000] BUG: mapping for 0x00b00000 at 0xc0900000 overlaps vmalloc space
664:<2>[ 23.566966] kernel BUG at /work2/ICS_8x25/kernel/fs/ext4/extents.c:1996!
665:<0>[ 23.573651] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
这里我搜索一个dmesg的log文件,里面有一个kernel BUG信息,通过grep命令我知道了要找的信息位于664行附近。
然后我希望查看这附件的所有内容,此时可以使用sed命令达到目的:
$sed -n '650,700p' snow_dmesg.log
<3>[ 16.800376] init: starting 'atfwd_log'
<3>[ 16.800983] init: starting 'atfwd'
<3>[ 21.821501] init: starting 'atfwd_log'
<3>[ 21.822424] init: starting 'atfwd'
<2>[ 23.529703] EXT4-fs error (device mmcblk0p13): ext4_valid_block_bitmap:282: comm er.ServerThread: Invalid block bitmap - block_group = 10, block = 327681
<3>[ 23.530108] Aborting journal on device mmcblk0p13-8.
<2>[ 23.531563] EXT4-fs (mmcblk0p13): Remounting filesystem read-only
<2>[ 23.531753] EXT4-fs error (device mmcblk0p13) in ext4_free_blocks:4643: Journal has aborted
<2>[ 23.533381] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<2>[ 23.535013] EXT4-fs error (device mmcblk0p13) in ext4_ext_remove_space:2669: Journal has aborted
<2>[ 23.537513] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<2>[ 23.546488] EXT4-fs error (device mmcblk0p13) in ext4_orphan_del:2120: Journal has aborted
<2>[ 23.554731] EXT4-fs error (device mmcblk0p13) in ext4_reserve_inode_write:5641: Journal has aborted
<0>[ 23.564124] ------------[ cut here ]------------
<2>[ 23.566966] kernel BUG at /work2/ICS_8x25/kernel/fs/ext4/extents.c:1996!
<0>[ 23.573651] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP
<4>[ 23.579118] Modules linked in: sm_event_driver sm_event_log
<4>[ 23.584676] CPU: 0 Tainted: G W (3.0.21-perf #1)
<4>[ 23.590413] PC is at ext4_ext_put_in_cache+0x14/0x48
<4>[ 23.595353] LR is at ext4_ext_map_blocks+0x2e0/0x14bc
<4>[ 23.600386] pc : [<c01ff260>] lr : [<c0202a08>] psr: 60000113
<4>[ 23.600391] sp : df647a10 ip : 00000000 fp : df232254
<4>[ 23.611848] r10: 00000000 r9 : 00000000 r8 : 00000000
<4>[ 23.617053] r7 : 00000000 r6 : df71ac20 r5 : df647b80 r4 : 00000000
<4>[ 23.623561] r3 : 00000000 r2 : 00000000 r1 : 00000000 r0 : df2322d0
OK,我已看到到希望查看的内容了。
相关文章推荐
- grep,sed,awk工具的使用
- awk-grep-sed简单使用总结(正则表达式的应用)
- (管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)
- grep和sed配合替换文件中的字串
- shell 分析log 使用例子 grep,sed,sort,awk运用
- linux下使用find xargs grep查找文件及文件内容,sed,gawk常用处理
- grep,sed,awk简单使用说明
- 使用方便 正则表达式grep,sed,awk(一)
- bash还能使用正则表达式,以前都没留意过,都是用外部程序 sed, grep来完成的
- grep、sed和awk命令的简单使用
- 使用grep判断是否需要通过sed往文件中增加数据。
- grep、sed、awk使用场所
- sed,awk,grep使用小结
- shell常用工具使用——awk、grep以及sed
- struts Action 和 html中框架的配合使用一例
- 详解linux,macos下批处理精要 grep,sed,find,正则,管道等结合使用例子
- grep和sed配合替换文件中的字串
- windows下面使用awk、sed、grep、gnuplot
- linux grep 和 sed使用
- linux如何查看一段时间的日志sed和grep的使用