0

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.

1
  • 1
    What do you mean by 100th of a second output? Do you really want the whole 8.591220.98 token, or just the portion after the last .? Given that 8.591220.98 is not a valid number, what exactly does it represent? Commented Mar 31, 2014 at 22:01

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You don't want to use brackets for sets in tr, they are not required and may lead to unexpected results depending on the string in question and its parsing. Consider: tr '.-' '12' <<< '._[]?=' => 1_[]?= vs. tr '[.-]' '[12]' <<< '._[]?=' => 1_]]]]. You only want to use them for character classes.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.