您的位置:首页 > 其它

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,我已看到到希望查看的内容了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: