1

I have a problem in R. I have a file file with this record:

[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ]

and I would like to remove the "[" and "]" and transform the row in this way (like a CSV):

2017-10-30T15:40:52.158521+01:00;XFM;ERROR:1;EPMHFM-00832;XFM;ecid: ;File: CXdsSessionManager.cpp;Line: 840;userId: ;

How I can do this?

Nathan Werth
  • 5,093
  • 18
  • 25

3 Answers3

0

using stringr package

library(stringr)    
your_var = str_replace_all(your_var, "\\[", "")
your_var = str_replace_all(your_var, "\\]", ";")

or

your_var = str_replace_all(your_var, "\\[|\\]", "")
write.csv(your_var, your_path)
Felipe Alvarenga
  • 2,572
  • 1
  • 17
  • 36
  • Sorry, what is your_var ? – Livio Colombo Dec 01 '17 at 15:29
  • `your_var = "[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ]"` or replace `your_var ` with the variable inside your data.frame, like `df$variable = str_replace_all...` – Felipe Alvarenga Dec 01 '17 at 15:55
  • Sorry, I don't understand, I have a dataframe with only one column – Livio Colombo Dec 01 '17 at 16:02
  • if you have a data.frame called `df`, with one variable called `x`, you just have to do `df$x = str_replace_all(df$x, "\\[", "")` and `df$x = str_replace_all(df$x, "\\]", ";")` – Felipe Alvarenga Dec 01 '17 at 18:21
0

You can do this with gsub as well. update: If you have only one column where you need to apply the replacement function. Then apply the gsub , replace x in below with df$colname. Where df is your dataframe and colname is the name of the column you are trying to apply the gsub function.

x = "[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832]
     [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId:  ]"

# replace '[' with "" in the x variable
result = gsub("(\\[)", "", x)
# replace ']' with ";" in the result variable
result = gsub("(\\])", ";", result)
result

output

2017-10-30T15:40:52.158521+01:00; XFM; ERROR:1; EPMHFM-00832;
XFM; ecid: ; File: CXdsSessionManager.cpp; Line: 840; userId:  ; 
user5249203
  • 4,436
  • 1
  • 19
  • 45
-1

[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ] [appName: ONE] [pid: 14264] [tid: 8492] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 8004024B] [srcException: XfmExc] [errType: 1] [dbUpdate: 2] [11.1.2.4.203.5250] The session has become invalid, a logoff is needed. [[]]

[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ] [appName: ONE] [pid: 14264] [tid: 8492] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 8004024B] [srcException: XfmExc] [errType: 1] [dbUpdate: 2] [11.1.2.4.203.5250] The session has become invalid, a logoff is needed. [[]]

[2017-10-30T15:40:52.158521+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ] [appName: ONE] [pid: 14264] [tid: 8492] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 8004024B] [srcException: XfmExc] [errType: 1] [dbUpdate: 2] [11.1.2.4.203.5250] The session has become invalid, a logoff is needed. [[]]

[2017-10-30T15:40:52.174141+01:00] [XFM] [ERROR:1] [EPMHFM-07274] [XFM] [ecid: 00iipfc_C4AFw00Fzzw0w00001iK002EVK] [File: CXdsSessionManager.cpp] [Line: 1316] [userId: ] [appName: ONE] [pid: 14264] [tid: 14932] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 80040275] [srcException: XfmExc] [errType: 1] [dbUpdate: 1] [11.1.2.4.203.5250] Invalid session ID. Your session may have expired, or your administrator may have logged you out. Please try to log in again. [[]]

[2017-10-30T15:40:52.174141+01:00] [XFM] [ERROR:1] [EPMHFM-07274] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 1316] [userId: ] [appName: ONE] [pid: 14264] [tid: 14932] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 80040275] [srcException: XfmExc] [errType: 1] [dbUpdate: 1] [11.1.2.4.203.5250] Invalid session ID. Your session may have expired, or your administrator may have logged you out. Please try to log in again. [[]]

[2017-10-30T15:40:52.189772+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ] [appName: ONE] [pid: 14264] [tid: 14932] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 8004024B] [srcException: XfmExc] [errType: 1] [dbUpdate: 2] [11.1.2.4.203.5250] The session has become invalid, a logoff is needed. [[]]

[2017-10-30T15:40:52.189772+01:00] [XFM] [ERROR:1] [EPMHFM-00832] [XFM] [ecid: ] [File: CXdsSessionManager.cpp] [Line: 840] [userId: ] [appName: ONE] [pid: 14264] [tid: 14932] [host: APP57GBFW] [nwaddr: 151.88.223.46:0;172.23.200.193:0;151.87.82.240:0;169.254.95.120:0;] [errorCode: 8004024B] [srcException: XfmExc] [errType: 1] [dbUpdate: 2] [11.1.2.4.203.5250] The session has become invalid, a logoff is needed. [[]] ....

This is my dataframe

  • you should edit the question. Not post it as an answer. Check this https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – user5249203 Dec 01 '17 at 16:09