ten1267
ten1267

Reputation: 15

Parse value from output using AWK

I need to parse the value, after the 100th of a second from output, from an FTP get command.

ftp> 8.591220.98disconnect

With assistance from Stack members I've been using the following to obtain the data:

ut1intput=$(awk 'NR==70{for(i=1;i<=NF;i++)if($i=="ftp>")print $(i+1)} filename.txt)

I'm using sed to strip the word "disconnect" from the output, but am perplexed as to how to print only the 100th of a second output, i.e.: 8.591220.98

Is awk the right tools for this task?

I'd appreciate any advice.

Upvotes: 0

Views: 95

Answers (2)

mklement0
mklement0

Reputation: 437100

To solve just the immediate problem:

If all you need is to extract the number-like token from your input data, you could simply use tr as follows:

tr -C -d '0-9.' <<<'ftp> 8.591220.98disconnect' # -> '8.591220.98'

But it makes more sense to integrate the desired operation into your original awk program, using sub(), as in @Håkon Hægland's answer:

ut1intput=$(awk '
  NR==70 {
    for(i=1;i<=NF;i++) { 
      if($i=="ftp>") {
        sub(/disconnect$/, "", $(i+1));  # remove 'disconnect' suffix
        print $(i+1)
      }
    }
  }' filename.txt)

Upvotes: 1

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40718

The awk command sub(/disconnect/,"", $i) will strip the word disconnect from field number $i. For instance:

echo "8.591220.98disconnect" | awk '{sub(/disconnect/,"", $1)}1'

gives:

8.591220.98

Upvotes: 0

Related Questions