Search for Pattern in Text String, then Extract Matched Pattern

时间:2017-08-04 12:04:49

标签: regex bash awk sed grep

I am trying to match and then extract a pattern from a text string. I need to extract any pattern that matches the following in the text string:

10289 20244

Text File:

KBOS 032354Z 19012KT 10SM FEW060 SCT200 BKN320 24/17 A3009 RMK AO2 SLP187 CB DSNT NW T02440172 10289 20244 53009

I am trying to achieve this using the following bash code:

Bash Code:

cat text_file | grep -Eow '\s10[0-9].*\s' | head -n 4 | awk '{print $1}'

The above code attempts to search for any group of approximately five numeric characters that begin with 10 followed by three numeric characters. After matching this pattern, the code prints out the rest of text string, capturing the second group of five numeric characters, beginning with 20.

I need a better, more reliable way to accomplish this because currently, this code fails. The numeric groups I need are separated by a space. I have attempted to account for this by inserting \s into the grep portion of the code.

3 个答案:

答案 0 :(得分:2)

grep solution:

grep -Eow '10[0-9]{3}\b.*\b20[0-9]{3}' text_file

The output:

10289 20244

  • [0-9]{3} - matches 3 digits

  • \b - word boundary

答案 1 :(得分:0)

awk '{print $(NF-2),$(NF-1)}' text_file

10289 20244

打印到最后一个和前一个。

答案 2 :(得分:0)

awk '$17 ~ /^10[0-9]{3}$/ && $18 ~ /^20[0-9]{3}$/ { print $17, $18 }' text_file

这将检查字段17的“10xxx”和字段18的“20xxx”,当两个匹配时,打印它们。