LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-17-2012, 06:42 AM   #1
xerox
Member
 
Registered: Mar 2011
Posts: 43

Rep: Reputation: 0
Unhappy Replacement using sed


Given repeated record like :

Code:
Object
dump
00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk
Object
dump
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk
Object
dump
00 90 55 44 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk
I want to extract
Code:
00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
from the first record, then the next
Code:
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
i tried
Code:
sed '/trace/,/dump/d'
but i'm getting all the info together

Code:
00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
00 90 55 44 00 00 ...
00 00 90 90 00 00 ...
Please help...
 
Old 01-17-2012, 07:41 AM   #2
arnow
LQ Newbie
 
Registered: Dec 2011
Location: Brooklyn
Distribution: fedora
Posts: 2

Rep: Reputation: Disabled
Can you show an example of the output you desire? If you could show what else you want to see besides the pairs of digits that would help. Meanwhile, let me suggest that filtering with grep can simplify your sed tasks.
 
Old 01-17-2012, 07:42 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,689

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
I think you need to explain further your desired output as what you have seems to fit what you require?
 
Old 01-17-2012, 07:42 AM   #4
sab0403
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
Here's an awk solution:

awk '$0 ~ /^dump/ {print "Title"; flag=1} {if($flag==0) { if($0 ~ /trace/) { flag=1 } else { print $0 } } }'

Output:

Quote:
Title
00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
Title
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
Title
00 90 55 44 00 00 ...
00 00 90 90 00 00 ...
 
Old 01-17-2012, 07:55 AM   #5
matthewetaft
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
I was going to say the same thing It seems like, based on what you have explained, that sed is doing what it should. It sounds like you want a record looking like this:

Object
dump
00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk
Object
dump
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk
Object
dump
00 90 55 44 00 00 ...
00 00 90 90 00 00 ...
trace:
[<sss>]lslsl
[<lsl>]ksksk

To end up looking like this:

00 90 00 00 00 00 ...
00 00 90 90 00 00 ...
00 90 45 23 00 00 ...
00 00 90 90 00 00 ...
00 90 55 44 00 00 ...
00 00 90 90 00 00 ...


To test this, I created a text file (test.txt) with your original example, then used "cat" while piping your sed command. Testing your original command like this:

cat test.txt |sed '/trace/,/dump/d'

did what it seems like you would want it to do, except it left the top two lines of the original file. In order to remove those two lines also, I modified the command to use sed twice like this:

sed '/trace/,/dump/d' | sed '/Object/,/dump/d'

Then it removed everything except for the lines between dump and trace. Is this what you're looking for? If not, please explain more.
 
Old 01-17-2012, 10:43 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
@matthewetaft: Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Actually, it sounds to me like he wants each section to be separated by at least one blank line. This can be done by telling sed to append a line (blank or otherwise) to the matched section before deleting it.

There's also no need to use two sed commands; just use one with multiple "-e" options.

Code:
sed -e '/trace/,/dump/ {/dump/ a
; d}' -e '/Object/,/dump/ { /dump/ a
; d}' inputfile
Unfortunately one of sed's limitations is that the only way it knows where to terminate the (a)ppend and (i)nsert commands is with a literal newline, so this particular command must span three lines.

It is also possible to reduce it to a single expression, however:

Code:
sed -r -e '/(trace|Object)/,/dump/ {/dump/a
; d}' inputfile
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed character replacement XXLRay Linux - General 6 04-27-2011 08:35 AM
replacement with sed disruptive Programming 7 08-11-2010 12:36 PM
Sed pattern replacement icyrail Programming 3 10-27-2009 06:33 AM
Sed column replacement. keysorsoze Programming 11 12-01-2008 10:50 AM
replacement with sed DeepSeaNautilus Programming 6 10-01-2008 07:48 AM


All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration