1

I have a file like this

Sampleoutput1[Fri Jun 10 06:11:59 2016]Local/ESSBASE0///139929633446208/Error(10
Sampleoutput1Local/ESSBASE0///139929633446208/[Fri Jun 11 06:11:59 2016]Error(10

I need to extract date field and add this date as column with delimited ($).Date can be present anywhere in the file.

[Fri Jun 10 06:11:59 2016]$Sampleoutput1[Fri Jun 10 06:11:59 2016]Local/ESSBASE0///139929633446208/Error(10
[Fri Jun 11 06:11:59 2016]$Sampleoutput1Local/ESSBASE0///139929633446208/[Fri Jun 11 06:11:59 2016]Error(10
Naresh
  • 191
  • 1
  • 11
  • Sorry . I am not able to use format code. Please dont mind – Naresh Aug 01 '16 at 20:49
  • Why grep -o '\[\(\w\{3\} \)\{2\}[0-9: ]\{14,16\}\]' didn't satisfy you? – Costas Aug 01 '16 at 21:00
  • Thanks Costas. Its working. is it possible to add this date with delimiter($) to the input file like [Fri Jun 10 06:11:59 2016]$Sampleoutput1[Fri Jun 10 06:11:59 2016]Local/ESSBASE0///139929633446208/Error(10 – Naresh Aug 01 '16 at 21:07

1 Answers1

2

Assuming the date is always enclosed in [ ... ] and that's the first [ on the line we can use a number of methods. Two common ones I use:

Using two cut commands (the lazy option, but it's very easy to understand what it does):

cut -d'[' -f2 $srcfile | cut -d ']' -f1

The standard sed option, using a simple regex:

sed 's/.*\[\([^]]*\)].*/\1/' $srcfile

If your output is more complicated with multiple [ characters in it then you'll need to use the sed option with a more complicated string:

sed 's/.*\[\(... ... .. ..:..:.. ....\)].*/\1/' $srcfile

EDIT: The revised question wants this added to the front of the string, leaving the rest of the line untouched. So we use the sed variant with a minor change:

sed 's/\(.*\[\(... ... .. ..:..:.. ....\)].*\)/[\2]$\1/' $srcfile

Result:

[Fri Jun 10 06:11:59 2016]$Sampleoutput1[Fri Jun 10 06:11:59 2016]Local/ESSBASE0///139929633446208/Error(10
[Fri Jun 11 06:11:59 2016]$Sampleoutput1Local/ESSBASE0///139929633446208/[Fri Jun 11 06:11:59 2016]Error(10
  • thanks Harris. is it possible to add this date with delimiter($) to the input file like [Fri Jun 10 06:11:59 2016]$Sampleoutput1[Fri Jun 10 06:11:59 2016]Local/ESSBASE0///139929633446208/Error(10 – – Naresh Aug 01 '16 at 21:18
  • See edit'd answer – Stephen Harris Aug 01 '16 at 21:24