arnpry
arnpry

Reputation: 1141

Search for Pattern in Text String, then Extract Matched Pattern

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.

Upvotes: 2

Views: 156

Answers (3)

valrog
valrog

Reputation: 236

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

This will check field 17 for "10xxx" and field 18 for "20xxx", and when BOTH match, print them.

Upvotes: 0

Claes Wikner
Claes Wikner

Reputation: 1517

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

10289 20244

Prints next to last and the one previous.

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

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

Upvotes: 2

Related Questions