Someone help please. I tried to do it with findstr but I couldn't, so now I'm trying to output the following numbers from this text file with gawk (what I need is in bold down below):
Analyzing pool.ntp.org (1 of 1)...
delayoffset from local clock
Stratum: 2
Warning:
Reverse name resolution is best effort. It may not be
correct since RefID field in time packets differs across
NTP implementations and may not be using IP addresses.
pool.ntp.org [155.101.3.115:123]:
ICMP: 73ms
NTP: +0.0162516s RefID: 127-67-113-92.pool.ukrtel.net [92.113.67.127]
What I want to output is in bold. How can I do this with Gawk? With my noob knowledge I can only output:
+0.0162516s RefID
But I would like to just have
0.0162516
Is this possible?
Ygor
February 22, 2011, 1:44am
2
I'm guessing that you're using something like...
$ awk -F: '$1=="NTP"{print $2}' file1
+0.0162516s RefID
If so, then just add gsub...
$ awk -F: '$1=="NTP"{gsub(/[^0-9\.]*/,"",$2);print $2}' file1
0.0162516
I appreciate the response.
The command I used was:
gawk -F":" "/NTP/{print $2}" time.txt
I can't get your code to work, it is probably because I'm using "gawk for windows".. Gawk for Windows
When I use your code I get this response:
Escape the quotes like:
awk -F":" "$1==\"NTP\"{gsub(/[^0-9\.]*/,\"\",$2);print $2}" file
echo "pool.ntp.org[155.101.3.115:123]:
ICMP: 73ms
NTP: +0.0162516s RefID: 127-67-113-92.pool.ukrtel.net [92.113.67.127]" |awk '/NTP/{print gensub(/(.*NTP: \+)(.*[0-9]s)
(Ref.*)/,"\\2",1,$0)}'
0.0162516s
Thanks Franklin52 , but I just get a blank output.
---------- Post updated at 02:48 AM ---------- Previous update was at 02:46 AM ----------
I have no idea how to use your code yinyuemi lol.
awk '/NTP/{print gensub(/(.*NTP: \+)(.*[0-9]s)(Ref.*)/,"\\2",1,$0)}' yourfile
Strange..this is what I get as output (with gawk windows):
G:\gawk>type file
pool.ntp.org[155.101.3.115:123]:
ICMP: 73ms
NTP: +0.0162516s RefID: 127-67-113-92.pool.ukrtel.net [92.113.67.127]
G:\gawk>gawk -F":" "$1==\"NTP\"{gsub(/[^0-9\.]*/,\"\",$2);print $2}" file
0.0162516
Weird... could you upload me your gawk.exe lol .
Maybe I don't have gsub installed? I would suppose it'd give me an error if that was the reason..
You can get the latest version here:
Gawk for Windows
Yep that's what I'm using..:wall:
Ygor
February 22, 2011, 3:36am
12
Maybe put the awk code into a file, e.g. prog.txt then call using -f
awk -F: -f prog.txt time.txt
No. That gives me some warning escape sequence error.
ctsgnb
February 22, 2011, 4:21am
14
sed '/^NTP:/!d;s:[^0-9]*\([0-9\.]*\).*:\1:' infile
Thanks for the reply ctsng, but I still get a blank output. What's going on?
Ygor
February 22, 2011, 9:01pm
16
Try putting the code that I supplied earlier into a file, not the subsequent variations that use escapes.
$1=="NTP"{gsub(/[^0-9\.]*/,"",$2);print $2}
Otherwise install Cygwin, which gives you a bash shell under Windows and some utilities like gawk.
You could also try this simplified awk script without " in the program (BTW probably best to keep the sign on the front as adjustments can be negative too):
C:\home\cubler> gawk -F "[s ]" "/^NTP:/ { print $2 }" time
+0.0162516
Without +/- sign:
C:\home\cubler> gawk -F "[s+-]" "/^NTP:/ { print $2 }" time
0.0162516
I prefer to use grep
grep -o "[+-]\?[0-9]\+\.[0-9]\+s" ufile
1 Like
Everyone's gawk solutions look great but they keep giving me blank outputs..
This the best one I've got so far, my output is:
+0.0162516s
Is there a way we can exclude the + and s?
---------- Post updated at 03:24 AM ---------- Previous update was at 03:18 AM ----------
Yes. It has been done. Thank you guys. Especially justlooks. Thank you
!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!
grep -o "[0-9]\+\.[0-9]\+s" time | tr -d 's'