How to remove the milliseconds from timestamps with sed?

前端 未结 5 1742
迷失自我
迷失自我 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:02

    This might work for you:

     sed 's/\....//;s/\....//' input_file.csv >output_file.csv
    
    0 讨论(0)
  • 2021-01-27 04:14
    sed 's/\(:[0-9][0-9]\)\.[0-9]\{3\}/\1/g' input_file.csv > output.csv
    

    You were almost there. In classic sed, you have to use backslashes in front of parentheses and braces to make them into metacharacters. Some versions of sed may have a mechanism to invert operations, so that the braces and parentheses are metacharacters by default, but that's not reliable across platforms.

    Also (strong recommendation): use single quotes around the sed command. Otherwise, the shell gets a crack at interpreting those backslashes (and any $ signs, etc) before sed sees it. Usually, this confuses the coder (and especially the maintaining coder). In fact, use single quotes around arguments to programs whenever you can. Don't get paranoid about it - if you need to interpolate a variable, do so. But single-quoting is generally easier to code, and ultimately easier to understand.

    I chose to work on just one time unit; you were working on three. Ultimately, given systematically formed input data, there is no difference in the result - but there is a (small) difference in the readability of the script.

    0 讨论(0)
  • 2021-01-27 04:14

    Try:

    sed 's,\(:[0-9]\{2\}\).[0-9]\{3\},\1,g'
    

    Also, try \d instead of [0-9], your version of sed may support that.

    0 讨论(0)
  • 2021-01-27 04:14

    You were near but some characters are special in sed (in my version, at least): {, }, (, ), but not :. So you need to escape them with a back-slash.

    And \1 takes expression between paretheses, it should be the first part until seconds, not the second one.

    A modification of your version could be:

    sed "s/\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\)\.[0-9]\{1,3\}/\1/g" input_file.csv > output.csv
    
    0 讨论(0)
  • 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<NF;i+=1) 
    if (i==2||i==3) {sub(/\..*/,"",$i);print $i} 
    else print $i;printf $NF"\n"}' inputfile
    12/13/2011,07:14:13,12/13/2011 07:14:13,231.56.3.245,LasVegas,US
    

    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 <NF we just print out $NF which is the last field. This won't cause a , to be printed after last field.
    0 讨论(0)
提交回复
热议问题