Search code examples
perlawksednumbershead

search and select decimal numbers in a text file line


I have xml textfiles which contain lines of multiple numbers (3) separated by tabs/spaces, from which I would like to select the each set of numbers separately.

From:

<tagname1>     110.0912    99.1234     55.1326   </tagname1>

Result:

110.0912

and:

99.1234

and:

55.1326

I would like to use sed, awk, grep, etc. perl is fine too. Seems simple, but can't figure out a cleaner line. I've tried:

more FILENAME | grep tagname1 | grep -E -o "[0-9]+*\.[0-9]+" |  head -n 1

Solution

  • awk -v which=2 '/<tagname1>(([0-9]*(\.[0-9]*)?)|[ \t])*<\/tagname1>/ {print $(which+1)}' input.txt
    

    Select which number you want to be printed using the variable which in this example it will print the second number which=2

    input.txt:

    <tagname1>     110.0912    99.1234     55.1326   </tagname1>