Computing.Net > Forums > Unix > sed - How to do conditional line in

sed - How to do conditional line in

Reply to Message Icon

Original Message
Name: jlhughey
Date: October 17, 2006 at 10:05:54 Pacific
Subject: sed - How to do conditional line in
OS: AIX 5.3
CPU/Ram: 2.3 GHz / 2 TB
Model/Manufacturer: IBM/System p595
Comment:

I am running a text file through a sequence of sed scripts, but one clean operation escapes me.

I want conditionally insert a blank line between *any two* uniquely indentifiable lines, *only if* a non-unique comment is *not* between them. I know in advance what the unique starting patterns of these lines (x, y, z below) are. A maximum of one comment line is between the unique lines.

Alternatively, I suppose I could insert blank lines between every line, then conditionally delete them in the next sed run; however let's try the insert scheme if possible.

Here are a couple of examples:

Legend:

x, y, z "Label" lines with unique and sequential labels (ie - 01160 , 01234 title2)
c a line with random comments.
b a blank line, or a single character such as a pipe "|"

Example 1:

Before:
x
y
z

After:
x
b
y
b
z
Example 2:

Before:
x
c
y
z

After:
x
c
y
b
z

Thanks for your help!


Joseph


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: October 18, 2006 at 11:27:58 Pacific
Reply: (edit)

It looks like this problem is solved easily enough with a tool that allows processing a line at a time - such as awk or perl.

However, I think sed is not the best choice. Why? Because implementing if type comparisons in sed is not easy. Check out this link, IF/ELSE TESTING IN SED:

http://www.student.northpark.edu/pe...


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed - How to do conditional line in

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge