0

For the below values I have to replace the second and fourth "." by "," in the below file

input

1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28

desired intermediate output

    1.351364711,103.7319660,2010-01-01 00:00:00
    1.345529841,103.7372875,2010-01-01 00:00:49
    1.342955629,103.7455272,2010-01-01 00:01:42
    1.339694956,103.7520503,2010-01-01 00:02:28

I know awk gsub(/./,",") this replaces everything by comma.But I only need the columns to be seperated by "," . I also wanted to switch the third column in the first place after this.

desired final output

2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503
9
  • 1
    So, the 2nd and 4th periods become commas? Commented Feb 10, 2017 at 2:24
  • Yup exactly the second and fourth becomes commas Commented Feb 10, 2017 at 2:25
  • 2
    If that's the case, the question should clearly say so. Commented Feb 10, 2017 at 2:26
  • final expected output is not clear, I think you want a space to separate field after time.. awk -F. '{print $5 " " $1 "." $2 "," $3 "." $4}' Commented Feb 10, 2017 at 2:35
  • 1
    @RKR why don't you post a original file contents and the command you used to extract the required fields. so we can offer a slight changes in your command itself. Commented Feb 10, 2017 at 2:59

2 Answers 2

2
bash-4.1$ cat file
1.351364711.103.7319660.2010-01-01 00:00:00
1.345529841.103.7372875.2010-01-01 00:00:49
1.342955629.103.7455272.2010-01-01 00:01:42
1.339694956.103.7520503.2010-01-01 00:02:28

bash-4.1$ awk -F. '{print $NF,$1"."$2,$3"."$4}' OFS=, file
2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503
1

Using sed:

$ sed 's/\./,/4; s/\./,/2' file
1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28

This first replaces the 4th dot with a comma, then the 2nd.

We could have done the substitutions in the opposite order too, but since replacing the 2nd dot removes a dot from the line, the dot originally being 4th becomes the 3rd dot:

$ sed 's/\./,/2; s/\./,/3' file
1.351364711,103.7319660,2010-01-01 00:00:00
1.345529841,103.7372875,2010-01-01 00:00:49
1.342955629,103.7455272,2010-01-01 00:01:42
1.339694956,103.7520503,2010-01-01 00:02:28

The s/.../.../n syntax (where n is a digit) substitutes the n:th matching occurrence on a line.

Moving the last column to the front may be done with

s/^\(.*\),\([^,]*\)/\2,\1/

I.e., match stuff from the start of the line up to a comma, then the comma, then stuff that contains no more commas.

So the full command may be

$ sed 's/\./,/4; s/\./,/2; s/^\(.*\),\([^,]*\)/\2,\1/' file
2010-01-01 00:00:00,1.351364711,103.7319660
2010-01-01 00:00:49,1.345529841,103.7372875
2010-01-01 00:01:42,1.342955629,103.7455272
2010-01-01 00:02:28,1.339694956,103.7520503

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.