regex - How to sed to capture the rightmost string? -


i have different environments. in each environment logs located in different path. e.g.:

/u01/../etc/apps/../def-20150626044921.log  /u01/log02/../etc/apps/../mno-20150626071656.log  /u02/../etc/apps/../xyz-20150626044921.log 

i trying grab rightmost digits before .log , display them in yyyy-mm-dd hh:mm:ss format.

using these in different combinations result individually. e.g.:

sed "s/01//";  sed "s/[^0-9]*//g";  sed "s/(.{4})(.{2})(.{2})/\1-\2-\3 /",  sed "s/(.{10})(.{3})(.{2})/\1 \2:\3:/"; 

my requirement add command in script. however, i'm unable results when path different. if instead of u01, it's u02 command doesn't work.

basically need capture yyyymmddhhmmss before .log , remove else. there way make command dynamic independent of log path , should not matter logs located, show date , time in desired format.

thanks looking @ question...!!

try this:

sed 's/.*\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).log$/\1-\2-\3 \4:\5:\6/' infile 

explanation:

  • .* matches until first digit, don't capture cause don't need it.
  • \([0-9]\{4\}\) captures year
  • the following 5 occurrences of \([0-9]\{2\}\) capture month, day, hour, minute , second.
  • .log$ after number match file extension , end of line, again don't capture cause don't need it.
  • then sub them desired format \1-\2-\3 \4:\5

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -