3

I have this line:

08:30:02.480507 IP 192.168.100.150.65119 > 192.168.100.151:53 59865 [1au] A? click.em.redbox.com. (48)

I need to cut out the ".480507" in the timestamp so the line looks like this:

08:30:02 IP 192.168.100.150.65119 > 192.168.100.151.53: 59865 [1au] A? click.em.redbox.com. (48)

I've fumbled around with several awk and cut statements but to no avail. I'd like to find an awk one liner but sed may be a better solution for this. I just don't know quite enough about either one when it comes to cutting in this fashion.

NOTE: This is an example, and the portion of the timestamp I need to cut out will not be the same, as I need to perform this action on a number of lines all with different timestamps.

1

With awk probably the simplest way is to do a regular expression substitution on the first whitespace-separated field, replacing everything from the period to the end of the field:

awk '{sub(/\..*/,"",$1)}1' somefile
  • Yes! exactly what I needed. – user53029 Jun 21 '16 at 14:52
2

I would use awk

awk --posix '{ gsub(/\.[[:digit:]]{6}/, "", $1); print }' filename

Will target the first field (space delimited) and search for a . followed by 6 numbers and empty it out.

0

with GNU sed

sed -r 's/^([^.]+)\.[0-9]+ /\1 /' filename
  • ^([^.]+) capture starting string upto first dot character
  • \.[0-9]+ match dot character followed by more than 1 digit characters

and if number of characters is consistent as given in the example,

sed -r 's/^(.{8}).{7} /\1 /' filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.