How to remove the milliseconds from timestamps with sed?

前端 未结 5 1746
迷失自我
迷失自我 2021-01-27 03:48

My input file is as follows:

12/13/2011,07:14:13.724,12/13/2011 07:14:13.724,231.56.3.245,LasVegas,US

I wish to get the following:

<         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-27 04:26

    Since the sed solution has already been posted, here is an alternate awk solution:

    [jaypal:~/Temp] cat inputfile
    12/13/2011,07:14:13.724,12/13/2011 07:14:13.724,231.56.3.245,LasVegas,US
    
    [jaypal:~/Temp] awk -F"," -v ORS="," '
    {for(i=1;i

    Explanation:

    1. Set the Field Separator to , and Output Record Separator to ,.
    2. Using a for loop we will loop over each fields.
    3. Using an if loop we would do substitution to the fields when the for loop parses over second and third fields.
    4. If the fields are not 2nd and 3rd then we just print out the fields.
    5. Lastly since we have used the for loop for we just print out $NF which is the last field. This won't cause a , to be printed after last field.

提交回复
热议问题