0

How can I remove nanosecond from every line in a file. data looks like filename is test.csv

ip,time,name
1.1.1.1,2018-08-17 15:05:52:016469121,1.13.0-0007
1.1.1.2,2018-08-17 15:05:52:016469121,1.13.0-0007

2 Answers 2

0

Try

sed 's/:[[:digit:]]*,/,/' file
ip,time,name
1.1.1.1,2018-08-17 15:05:52,1.13.0-0007
1.1.1.2,2018-08-17 15:05:52,1.13.0-0007

it will replace the last colon, following digits, and final comma with just the comma.

1
  • Thanks RudiC, it worked and this looks more good what I was looking.
    – Syed Rizvi
    Commented Aug 17, 2018 at 10:27
0
awk -F',' -vOFS=',' '{sub(/:[0-9]{9}$/, "", $2); print}' input.txt

Output:

ip,time,name
1.1.1.1 2018-08-17 15:05:52 1.13.0-0007
1.1.1.2 2018-08-17 15:05:52 1.13.0-0007

Explanation

  • awk -F',' -vOFS=',' '{foo}' input.txt: use awk with the field separator (and output field separator) ,, running commands foo, on file input.txt.
  • sub(/:[0-9]{9}$/, "", $2);: for the second field $2, replace the regular expression :[0-9]{9}$ (i.e. nine digits in a row at the end of the field) with nothing.
  • print: print the new line
2
  • Thanks for your help. It is removing the comma(,) between time and name
    – Syed Rizvi
    Commented Aug 17, 2018 at 10:16
  • @SyedRizvi Fixed
    – Sparhawk
    Commented Aug 17, 2018 at 10:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .