[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below.

2018.07.01, Sunday
09:27 some text 123456789 0 21 0.06 0.07 0.00
2018.07.02, Monday
09:31 some text 123456789 1 41 0.26 0.32 0.00
09:39 some text 456789012 1 0.07 0.09 0.09
09:45 some text 932469494 1 55 0.29 0.36 0.00
16:49 some text 123456789 0 48 0.12 0.15 0.00
20:13 some text 123456789 0 46 0.12 0.15 0.00

I want the sed command to find the pattern YYYY.MM.DD, Day of week (Y-year, M-month, D-Day, e.g. 2018.07.01, Sunday) and:

  1. to copy the pattern to the buffer
  2. to remove the pattern from the given line and then
  3. to past the pattern into the line below so that you get the effect below
2018.07.01, Sunday 9:27 am some text 123456789 0 21 0.06 0.07 0.00
2018.07.02, Monday 9:31 some text 123456789 1 41 0.26 0.32 0.00
2018.07.02, Monday 09:39 some text 456789012 1 0.07 0.09 0.09
2018.07.02, Monday 9:45 some text 932469494 1 55 0,29 0,36 0,00
2018.07.02, Monday 16:49 some text 123456789 0 48 0.12 0.15 0.00
2018.07.02, Monday 20:13 some text 123456789 0 46 0.12 0.15 0.00

I will be grateful for constructive answers.

Welcome to the forum.

Any attempts / ideas / thoughts from your side? Where does the "am" in the first output line come from?
Similar problems have been solved umpteen times in here - did you try searching?

I tried to use sed command as below (copied from a one tutorial) but I can not adjust sed to my issue.

 sed -e '1!G;h;$!d' forward.txt > backward.txt

The file includes billing phone number after export from pdf to text file.

I searched similar problems but results are away from my perespective.

Would be an easy thig to do with awk ; but as you insist on sed , try

sed -rn '/[0-9]{4}(\.[0-9]{2}){2}, [[:alpha:]]+/ {h;n;}; H; x; s/\n/\t/; p; G; s/\n/\t/; s/(.*)(\t.*)\2/\1/; x; ' file
2018.07.01, Sunday    09:27 some text 123456789 0 21 0.06 0.07 0.00
2018.07.02, Monday    09:31 some text 123456789 1 41 0.26 0.32 0.00
2018.07.02, Monday    09:39 some text 456789012 1 0.07 0.09 0.09
2018.07.02, Monday    09:45 some text 932469494 1 55 0.29 0.36 0.00
2018.07.02, Monday    16:49 some text 123456789 0 48 0.12 0.15 0.00
2018.07.02, Monday    20:13 some text 123456789 0 46 0.12 0.15 0.00

Inserting a <TAB> in lieu of a space is necessary to identify the line added; not sure how to simplify. There may be more elegant solutions to come, however...

[quote=rudic;303021898]
Would be an easy thig to do with awk ; but as you insist on sed , try

sed -rn '/[0-9]{4}(\.[0-9]{2}){2}, [[:alpha:]]+/ {h;n;}; H; x; s/\n/\t/; p; G; s/\n/\t/; s/(.*)(\t.*)\2/\1/; x; ' file

Thanks a lot RudiC :b: 

I have more sed commands to execute as below. 


s/^$/d/
/^[a-zA-Z]/d

I want to use

sed -f script-file

but if I add your sed it does not work. Could your advice how I can putt all sed into script-file?

Moderator comments were removed during original forum migration.

script-file:

s/^$/d/
/^[a-zA-Z]/d
/[0-9]{4}(\.[0-9]{2}){2}, [[:alpha:]]+/ {h;n;}; H; x; s/\n/\t/; p; G; s/\n/\t/; s/(.*)(\t.*)\2/\1/; x; file

file:

2018.07.01, Sunday
09:27 some text 123456789 0 21 0.06 0.07 0.00
2018.07.02, Monday
09:31 some text 123456789 1 41 0.26 0.32 0.00
09:39 some text 456789012 1 0.07 0.09 0.09
09:45 some text 932469494 1 55 0.29 0.36 0.00
16:49 some text 123456789 0 48 0.12 0.15 0.00
20:13 some text 123456789 0 46 0.12 0.15 0.00

Bash command:

 sed -f script-file file

Error:

sed: file c.sed line 3: Invalid back reference

a) you dropped the necessary sed -rn options.
b) drop the trailing file in the long line.
c) in s/^$/d/ , there's an s and the trailing / too many.

1 Like

Thank you RudiC for your support.

For clarification: - it works.

The updated script-file:

s/^$/d/
/^[a-zA-Z]/d
/[0-9]{4}(\.[0-9]{2}){2}, [[:alpha:]]+/ {h;n;}; H; x; s/\n/\t/; p; G; s/\n/\t/; s/(.*)(\t.*)\2/\1/; x;

The command:

sed -rn -f script-file file

.

RudiC said
c) in s/^$/d/, there's an s and the trailing / too many.

You still have the s/^$/d/ that puts a d on empty lines. While /^$/d deletes empty lines.