Question

I have files with data in this format:

2014-02-05 09:43:30;data;data;data;data;data;
2014-02-05 09:52:10;data;data;data;data;data;
2014-02-05 10:02:41;data;data;data;data;data;

This data is already processed by searching for specific "data" patterns with a grep | awk syntax and produces an output in the same format as the original file data. This output is displayed on the screen.

What I'm interested in is the first part:

2014-02-05 09:43:30     

The server I'm running this on has the date in GMT (UTC) timezone and the data is also in this format. I want to take the above portion of data and substitute it like this:

  • remove 2014-02-05 09:43:30 from the generated output
  • feed the time part (09:43:30) to TZ=America/Washington date -d "09:43:30" (as a saved variable for each processed line) in order to get the output of date/time for the specified timezone (America/Washington)
  • reinsert the output of TZ=America/Washington date -d "09:43:30" into the original output (generated by the grep | awk), replacing the original date and processed time with the new date and time generated by the TZ=America/Washington date -d "09:43:30"
  • re-display the modified output

I am having issues with the syntax logic behind the process I want to implement, as described above in the bullets.

Was it helpful?

Solution

Try something like

#!/bin/bash
while read -r line; do
  first="${line%%;*}"
  second="${line#*;}"
  export TZ=EST
  echo $(date -d "$first GMT" '+%Y-%m-%d %H:%M:%S')";$second"
done < <(grep)

Example output

> abovescript 
2014-02-05 04:43:30;data;data;data;data;data;
2014-02-05 04:52:10;data;data;data;data;data;
2014-02-05 05:02:41;data;data;data;data;data;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top