I have a file with many lines. Some lines display the date and time, e.g. 2022-03-16-08:00
I want all lines following the pattern found to have that pattern prepended
In addition, because there are many lines with different times, I want this to change for every instance of the pattern, and prepend the following lines of that instance with the respective date&time.
For example, I have the following file (example.txt):
Date1: 2022-03-16-08:00
Something happened
Something else happened
Date2: 2022-03-16-08:10
Something happened
Something else happened
Something else happened
And the result I want is:
Date1: 2022-03-16-08:00
2022-03-16-08:00 Something happened
2022-03-16-08:00 Something else happened
Date2: 2022-03-16-08:10
2022-03-16-08:10 Something happened
2022-03-16-08:10 Something else happened
2022-03-16-08:10 Something else happened
I tried with sed to prepend the pattern found to each other line, but the sed variable doesn't seem to work:
sed -e '/2022-/s/\(.*\)/\1/' -e 's/^/$1/' example.txt
Result:
$1Date1: 2022-03-16-08:00
$1Something happened
$1Something else happened
$1Date2: 2022-03-16-08:10
$1Something happened
$1Something else happened
$1Something else happened
I thought that it may be feasible with awk, to take the pattern with awk -F: '/2022/{var=$2}' and then prepend it to the next lines, but I don't know how I would change it to the new instance of Date
Any help is appreciated and very welcome.