LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Replacement using sed (https://www.linuxquestions.org/questions/linux-newbie-8/replacement-using-sed-924201/)

xerox 01-17-2012 05:42 AM

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

arnow 01-17-2012 06:41 AM

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.

grail 01-17-2012 06:42 AM

I think you need to explain further your desired output as what you have seems to fit what you require?

sab0403 01-17-2012 06:42 AM

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

matthewetaft 01-17-2012 06:55 AM

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.

David the H. 01-17-2012 09:43 AM

@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



All times are GMT -5. The time now is 02:39 PM.