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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern
# 1  
Old 08-20-2018
[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below.

Code:
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


Code:
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.

Last edited by father_7; 08-20-2018 at 07:10 AM.. Reason: small language mistakes repeired
# 2  
Old 08-20-2018
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?

Last edited by RudiC; 08-20-2018 at 08:05 AM..
# 3  
Old 08-20-2018
Quote:
Originally Posted by RudiC
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.

Code:
 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.
# 4  
Old 08-20-2018
Would be an easy thig to do with awk; but as you insist on sed, try



Code:
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...
# 5  
Old 08-20-2018
Quote:
Originally Posted by RudiC
Would be an easy thig to do with awk; but as you insist on sed, try

[CODE]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 Smilie

I have more sed commands to execute as below.

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

I want to use

Code:
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?
# 6  
Old 08-20-2018
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
# 7  
Old 08-20-2018
script-file:

Code:
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:

Code:
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:

Code:
 sed -f script-file file


Error:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

2. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. UNIX for Dummies Questions & Answers

Finding the same pattern in three consecutive lines in several files in a directory

I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this: grep -Ril "News/U.S." . I can find which files contain the pattern "News/U.S." in a directory. I am unable to accomplish about how to extend this code so that it can... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

5. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

6. UNIX for Dummies Questions & Answers

Join lines on finding a pattern

I have a file with the following contents. DTP START START START DTP START DTP START DTP START I like to join the lines like this DTP START START START DTP START DTP START (2 Replies)
Discussion started by: nsuresh316
2 Replies

7. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

8. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

9. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

10. Shell Programming and Scripting

grep - to exclude lines beginning with pattern

11132 13069 11137 11142 13070 Can I use grep command to exclude all lines beginning with 13? I dont want to use grep -v 13 as potentially there will be a number with something like 11013 that I would exclude in error.. (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question