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!

How do I replace just the second pattern?

Status
Not open for further replies.

whatisthis987

IS-IT--Management
Apr 24, 2007
34
US
Hi,
I have a file looks like this:
aaa bbb ccc ddd;
123 321 423 444;
aaa eee ccc ddd;
aaa fff ccc ddd;
12243455465467;

I want the output to look like this:
aaa bbb;
123 321 423 444;
aaa eee;
aaa fff;
12243455465467;

Basically, just remove the last 2 entries of the lines begin with "aaa" but preserve the semicolon.

I don't see how I can add a secondary or/and third search patterns in sed or vi. Can someone please help?

Thanks.
 
Well, I just figured out how to do it with sed.
sed '/aaa/s/ ccc ccc;/;/g' FILE

but i have a more complicated case like this:

aaa bbb
222 333 444
eee ccc ddd;
eee ccc ddd;

I want it to be:
aaa bbb
222 333 444
eee;
eee ccc ddd;

which means a line always end with a semicolon ";". Any help?
 
You mean every line that ends with semicolon, remove last 2 fields? because in second example your last line is like that but you didn't alter it.

pls clarify...

if so:

[tt]sed '/;$/ s/ *[^ ][^ ]* *[^ ][^ ]* *;$/;/'[/tt]

HTH,

p5wizard
 
Sorry for the confusion. I had a typo in my example. No, not remove the last 2 fields of every line that ends with a semicolon.

I want to search between "aaa" and ";" for "ccc ddd" and replace it with blank. I corrected and here's another test case:

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;

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;


 
What about this ?
Code:
sed '/^aaa/s/ ccc ddd;$/;/' FILE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks 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?
 
So, you wanted just this ?
Code:
sed '/ ccc ddd;$/;/' FILE

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,
No, I don't want any other lines that begins with non-"aaa" to change.

For example:
basas sdsa asda ccc ddd;
should not change.

Just to simplify my test case:
aaa 111 222 ccc ddd;
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs ccc ddd;
basas sdsa asda ccc ddd;

The desired output is:
aaa 111 222;
aaa dfg dffd dfdfd
sdfdsf dfdf dfd fdgdfg
dfgdg dfgd dgfd gdgdf
fdsfs;
basas sdsa asda ccc ddd;

I hope it's clear this time. Thanks.
 
Well, seems easier with awk than sed.
Please, try my suggestion here:
thread271-1623267

If you insist with sed, you may try this:
Code:
tr ';\n' '\n;' <FILE | sed '/^;*aaa /s! ccc ddd$!!' | tr '\n;' ';\n'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV for the solution. The s! was giving me some problem so I modified it and added \s to remove the spaces before aaa.

tr ';\n' '\n;' <FILE | sed '/^;*\s*aaa /s/ ccc ddd$' | tr '\n;' ';\n'

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top