Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED - find and replace between patterns

Status
Not open for further replies.

whatisthis987

IS-IT--Management
Apr 24, 2007
34
US
Hi,
I would like to search between "aaa" and ";" for "ccc ddd" and replace it with a blank. To me, each "line" of interest begins with "aaa" and ends with a semicolon ";".

For example,
aaa 111 222 ccc ddd;
sdfs fdd ccc ddd;
sfsdf dfdf fdf dfff
dfsdfs fdfd;
aaa dfg ccc ccc ddd;
aaa ddd ccc ccc ddd;
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs ccc ddd;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
aaa 111 222 ccc ddd;

Desired output is:
aaa 111 222;
sdfs fdd ccc ddd;
sfsdf dfdf fdf dfff
dfsdfs fdfd;
aaa dfg ccc;
aaa ddd ccc;
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
aaa 111 222;

I tried, sed '/aaa/,/;/s/ ccc ddd//g' FILE but the 2nd line of the output is wrong. It shouldn't have changed.

aaa 111 222;
sdfs fdd;
sfsdf dfdf fdf dfff
dfsdfs fdfd;
aaa dfg ccc;
aaa ddd ccc;
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
basas sdsa asda ccc ddd;
aaa 111 222;


Any ideas?
 
Why not simply this ?
sed '/^aaa/s/ ccc ddd;$/;/' FILE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV's sed solution, which he already gave in your other thread (thread822-1623129) works for me...

But since you asked in the AWK forum:

[tt]
awk '/^aaa.*;$/{sub(" ccc ddd","",$0)} 1' /path/to/file
explain:
[red] /^aaa.*;$/
look for lines that start with aaa and end with ;[/red]
[blue] {sub(" ccc ddd","",$0)}
in these lines substitute " ccc ddd" with an empty string in the record ($0)[/blue]
[green] 1
default action: print (after substitution has taken place)[/green]
[/tt]


HTH,

p5wizard
 
Thanks PHV and p5wizard for the replies but I also want:

from:
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs ccc ddd;

to:
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs;

Any help?

 
OK, a line is terminated by a semicolon, right ?
Code:
awk 'BEGIN{RS=";"}!NF{next}/^[\n]?aaa /{sub(/ ccc ddd$/,"")}{printf "%s;",$0}END{printf "\n"}' FILE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top