how to grep data based on strings in different column?

时间:2017-12-18 05:29:57

标签: bash grep

I have aa.dat which have data like below,

frame     1 16980 O  17115H   17123H
FRAME     1 16980 O  16971H   16981H
frame     1 16982 O  17110H   17121H
FRAME     1 16982 O  16961H   16978H
frame     1 16984 O  17124H   17127H
FRAME     1 16984 O  16983H   16990H
frame     1 17110 O  17102H   17225H
FRAME     1 17110 O  16951H   17138H
FRAME     2 16982 O  16961H   16978H
frame     2 16984 O  17124H   17127H
FRAME     2 16984 O  16983H   16990H
frame     2 17110 O  17102H   17225H
FRAME     2 17110 O  16951H   17138H
...
frame     3 16984 O  17124H   17127H
FRAME     3 16984 O  16983H   16990H
frame     3 17110 O  17102H   17225H
FRAME     3 17110 O  16951H   17138H
frame     4 16980 O  17115H   17123H
FRAME     4 16980 O  16971H   16981H
frame     4 16982 O  17110H   17121H
FRAME     4 16982 O  16961H   16978H
frame     4 17110 Ac  17102H   17225H
FRAME     4 17110 Ac  16952H   17118H
...

I want to get data with "FRAME" and "17110" ONLY IN COLUMN 3.

FRAME     1 17110 O  16951H   17138H
FRAME     2 17110 O  16951H   17138H
FRAME     3 17110 O  16951H   17138H
FRAME     4 17110 Ac  16952H   17118H
...

However, my code

grep -E 'FRAME.*17110' aa.dat

could not separate data from line consisting of 17110H.

Any suggestion and help would be highly appreciated.

Best,

Leon

1 个答案:

答案 0 :(得分:0)

grep -P 'FRAME.*17110\W' aa.dat

Brief explanation,

  • -P: Interpret PATTERN as a Perl regular expression
  • Appended \W would allow only non-word (i.e. [^a-zA-Z_])