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>
</text><last-change-date>2015-04-06T15:23:46.813945Z</last-change-date><last-<cursor-position>-1</selection-bound-position></height></note>

I have partially succeeded with :

sed -n 's/.*oy\/size">\(.*\).*$/\1/p'
sed -n '/<\/note-content>/p'

Which gives me the first line OK,
no empty lines,
last line has the pattern included but not wanted!

If I could loop and also not print pattern this would be perfect, all with sed!
P.S. this is from gnote app, converting to text and...

Thank you for your time and help

Would this do:

sed -n 's/.*oy\/size">\(.*\).*$/\1/p; s/<\/note-content>//p' file
PDF converters
gpdftext and pdftotext

?

Yes, I sure can work with this.
I will dissect it and make it work the way I want!
I'm amazed at this little command and how versatile it is, the reason I want to get to know it!!

Thanks very much for your help Rudic

To include the empty line, try

sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; N; s/<\/note-content>//p; TL}' file
PDF converters

gpdftext and pdftotext

Ah, Got it!

I was trying to find the L switch, but it is a label called L working with T. If not find pattern then go back and repeat until pattern found !!

Great!

sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; N; s/<\/note-content>//p; TL}'

It looked daunting at first but then...

---------- Post updated at 11:32 AM ---------- Previous update was at 10:40 AM ----------

sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; N; s/<\/note-content>//p; TL}'
sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/;

Find pattern, cuts it out, s// finds nothing so prints remainder of pattern buffer. So it acts like the p

:L; N; s/<\/note-content>//p; TL}'

First pass N loads next line into pattern space, no match, unloads pattern space with p, blank line.
Goes back then finds pattern, which is deleted with // and then prints remainder with p
Next, EOF, so out.

Is this right the way I correlated the actions to the proper command sets?

Thanks

Almost, but not quite.

The let's call it "empty regex" uses the last one applied right before. In this case: reuse the address one to find the line. (Not easy to find in the docu. I didn't. I learned it from these forums.) Nothing is printed here.

Nothing is deleted. N just collects line after line into pattern space. The p flag to sed 's s(ubstitute) prints only if a substituition was made, i.e. when the pattern was found. Then also ignore the T command and leave.

sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; N;

So what your saying is:

Pattern space is going to have
1: PDF converters
2: ....the whole complete line

s//\1/;

takes only the first line from pattern space, which is, 1:
and gets printed, released somewhere around, hmmm, not sure where on that line, anymore!

The rest I think I get :wink:

Ok hope it makes sense.

Not quite. s//\1/; is executed once as it is outside the loop. \1 refers to the parenthesized part in the regex.
Try

sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; l; N; l; s/<\/note-content>//p; TL}' file 

to see the development of the pattern space.

1 Like
sed -n '/.*oy\/size">\(.*\).*$/ {s//\1/; :L; N; s/<\/note-content>//p; TL}'

sed -n '/.*oy\/size">\(.*$\)/ {s//\1/; :L; N; s/<\/note-content>//p; TL}'

I modified to the second line since it seemed redundant, unless it has a purpose I can't see. It works.

Ok now I get the \1 !!!

BTW thanks for the class and your patience, much appreciated.

Revised sed with proper result.

cat split | sed '/\(.*varchar.*[)] \)\(.*$\)/ {s//\1\n\2/}'

declare @newname varchar(30) 
Select @newname = filename from table_bck
declare @newname varchar(30) 
Select @newname = filename from table_bck
declare @newname varchar(30) 
Select @newname = filename from table_bck

Square braquets being the solution to parens in sed!!

---------- Post updated at 03:00 PM ---------- Previous update was at 02:55 PM ----------

Sorry wrong post !!!