Boat Drinks  

Go Back   Boat Drinks > General > Computer and Consoles

Reply
 
Thread Tools Display Modes
Old 20-06-2008, 17:03   #1
Goose
The list is long, but distinguished
 
Goose's Avatar
 
Join Date: Dec 2006
Location: Århus, Denmark
Posts: 1,643
Default I Sed, she sed

Does anyone here use sed much? I've got a bit stuck, could do with some help!
__________________
Goose is offline   Reply With Quote
Old 20-06-2008, 17:03   #2
Garp
Preparing more tumbleweed
 
Garp's Avatar
 
Join Date: Jun 2006
Location: Hawaii
Posts: 6,038
Default

Quote:
Originally Posted by Goose View Post
Does anyone here use sed much? I've got a bit stuck, could do with some help!
Whatcha trying to achieve? I use it on and off here.
__________________
Mal: Define "interesting"?
Wash: "Oh, God, oh, God, we're all gonna die"?
Garp is offline   Reply With Quote
Old 20-06-2008, 17:07   #3
Goose
The list is long, but distinguished
 
Goose's Avatar
 
Join Date: Dec 2006
Location: Århus, Denmark
Posts: 1,643
Default

Right, this is a simple example. In real times, my data is a bit hairy but the idea should work.

I have a file populated a little like this:

abcdefghijklmn<<o>>pqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefg<h>ijklmnopqrstuvwxyz
abcdefghijkl<<m>>nopqr<<s>>tuvwxyz
abcdef<g>hijklmnopqrs<<t>>uvwxyz
abcdefghijklmno<<p>>qrstuvwxyz


I need to use sed to extract out anything surrounded by << >>. So ideally I'd end up with:

o
m
s
t
p


It's frustrating me somewhat.
__________________
Goose is offline   Reply With Quote
Old 20-06-2008, 20:30   #4
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

The fact you can have more than one pattern per line caused me some major grief, but if it's OK to use sed twice, then I managed to get around that one:

Code:
sed '/\(<<[^<>]*>>\)/s//\n\1\n/g' | sed '/^<<\([^<>]*\)>>$/s//\1/p;d'
The first sed breaks out the <<...>> so that each is split out onto a line by itself, then the second one picks out just those lines.

Last edited by Mark; 20-06-2008 at 20:35.
Mark is offline   Reply With Quote
Old 20-06-2008, 20:40   #5
Goose
The list is long, but distinguished
 
Goose's Avatar
 
Join Date: Dec 2006
Location: Århus, Denmark
Posts: 1,643
Default

Thanks Mark. I've not used sed much, takes some getting used to.
__________________
Goose is offline   Reply With Quote
Old 20-06-2008, 20:43   #6
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

Make sure you use the latest edit - I spotted a few bits of redundant code in the original and removed them.
Mark is offline   Reply With Quote
Old 20-06-2008, 23:26   #7
Fayshun
Rocket Fuel
 
Fayshun's Avatar
 
Join Date: Jun 2006
Location: Adrift in the Orca
Posts: 6,845
Default

Quote:
sed '/\(<<[^<>]*>>\)/s//\n\1\n/g' | sed '/^<<\([^<>]*\)>>$/s//\1/p;d'
Such language on a family forum!
__________________

We must move forward not backward, upwards not forward, and always twirling, twirling, twirling...
Fayshun is offline   Reply With Quote
Old 21-06-2008, 03:13   #8
Mark
Screaming Orgasm
 
Join Date: Jul 2006
Location: Newbury
Posts: 15,194
Default

I do wonder what that would say when translated into semaphore and back.

Last edited by Mark; 21-06-2008 at 03:45.
Mark is offline   Reply With Quote
Old 23-06-2008, 15:34   #9
Goose
The list is long, but distinguished
 
Goose's Avatar
 
Join Date: Dec 2006
Location: Århus, Denmark
Posts: 1,643
Default

OK, second challange. This should be easy, I've nearly got it working but seem to be running in circles.

Say you have this:

Code:
{.One$="One",.Number=12,.Two$="Two",.Number=13,.Three$="Three",/
I need to grab everything between .Two$=" and the following "

A pint to whoever digs me out of sed hell #2.

I really should buy a book on this...
__________________
Goose is offline   Reply With Quote
Old 23-06-2008, 16:11   #10
Garp
Preparing more tumbleweed
 
Garp's Avatar
 
Join Date: Jun 2006
Location: Hawaii
Posts: 6,038
Default

http://www.amazon.co.uk/sed-awk-Pock...4233854&sr=8-2
__________________
Mal: Define "interesting"?
Wash: "Oh, God, oh, God, we're all gonna die"?
Garp is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 21:10.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.