Selecting a field after a string using awk -
i'm new awk having been introduced on weekend. have question i'm hoping may able me with.
how 1 select field follows specific string?
how expand code select more 1 field following specific string?
as example, given line in text file have
2 of 10 19/4/2014 school name random text distance 800m more random text time 2:20:22 winner someonefast. some attributes consistent can extract these fields. example 2, 10 , date. however, there lot of variable text before next field wish extract. hence question. using awk can extract next field following string? example i'm interested in fields following /distance/ or /time/ string in combination $1, $3, $4, $5.
your appreciated.
andy
basic:
awk '{ (i = 6; <= nf; ++i) { if ($i == "distance") distance = $(i + 1) if ($i == "time") time = $(i + 1) } print $1, $3, $4, $5, distance, time }' file output:
2 10 19/4/2014 school 800m 2:20:22 but it's not enough other significant texts still part of school name after $5. should add condition.
the better solution have delimiter besides spaces tabs , use \t fs.
Comments
Post a Comment