0

I have a file with the following kind of lines:

file.txt

# 0, 18540, Wed Oct 26 08:01:40 2014
`cd xyz`
# 0, 18571, Wed Nov 26 08:01:41 2014
`cd abc`
# 0, 18881, Wed Nov 26 08:01:55 2014
`mkdir lib`

Now, i want to replace the content of the file to something like this:

new_file.txt

2014/Oct/26 08:01:40 `cd xyz`
2014/Nov/26 08:01:41 `cd abc`
2014/Nov/26 08:01:55 `mkdir lib`

A script or shell command will be great.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Asish Pati
  • 103
  • 2
  • 8

1 Answers1

0

What about catching the 3rd field on odd lines and computing date like this:

$ date -d"Wed Oct 26 08:01:40 2014" +"%Y/%b/%d %T"
2014/Oct/26 08:01:40

Then, store the variable and print it back together with the full even line:

$ awk -F, 'NR%2{cmd ="date \"+%Y/%b/%d %T\" -d \""$3"\""
                cmd | getline var
                close (cmd)
                next
                }
           {print var, $0}' file

For your input it returns:

$ awk -F, 'NR%2{cmd ="date \"+%Y/%b/%d %T\" -d \""$3"\""; cmd | getline var; close (cmd); next} {print var, $0}' file
2014/Oct/26 08:01:40 `cd xyz`
2014/Nov/26 08:01:41 `cd abc`
2014/Nov/26 08:01:55 `mkdir lib`
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thats working fine with the example I had.Thanks. But can't we catch the lines starting with "#" rather than every odd line – Asish Pati Dec 01 '14 at 13:05
  • Yes, sure, replace `NR%2` with `$1 ~ /^#/` to indicate that the 1st field has to start with `#`.. – fedorqui Dec 01 '14 at 13:06