如何仅从以x开头的行中剪切字符?

时间:2013-02-26 10:39:03

标签: linux unix grep cut

我正在尝试使用cut -c-2-18- filename剪切第2和第18个角色之间的所有内容。这会在每一行上剪切指定的字符,但我只想从以>开头的行剪切它。

3 个答案:

答案 0 :(得分:1)

这样的东西
grep ^\> file | cut -c 2-18

应该这样做吗?

[92][11:46:21] vlazarenko@alluminium (~/tests) > grep ^\> file | cut -c 2-18
 asdf
 bcda
 ofpjg
[93][11:46:25] vlazarenko@alluminium (~/tests) > cat file
> asdf
> bcda
zxcv
lasdf
> ofpjg

答案 1 :(得分:1)

您可以单独使用GNU grep执行此操作:

$ cat file
>grep from 2 to 18 character line 1
>grep from 2 to 18 character line 2
grep from 2 to 18 character line 3
grep from 2 to 18 character line 4

$ grep -Po '(?<=^>).{17}' file
grep from 2 to 18
grep from 2 to 18

这使用positive lookbehind仅匹配以>开头的行,.{17}匹配接下来的17个字符。

awk

$ awk '/^>/{print substr($0,2,18)}' file
grep from 2 to 18 
grep from 2 to 18 

答案 2 :(得分:1)

awk '/^\>/{print substr($0,2,17)}' your_file