search two words in sed

I've following sed command working fine -
sed '/search_pattern1/ !s/pattern1/pattern2/" file

Now, I want to search two patterns - search_pattern1 and search_pattern2 .

How can put these into above sed statement ?

Thanks in advance.

sed '/search_pattern1\|search_pattern2/ !s/pattern1/pattern2/" file

Different sed dialects might not want the backslash before the | alternation operator.

This doesn't work.

Do you get a syntax error, or doesn't it do what you expect? Your request can be interpreted at least two ways (lines not containing search_pattern1 or search_pattern2 should have pattern1 replaced with pattern2; or, lines not containing search_pattern1 should have pattern1 replaced with pattern2, and lines not containing search_pattern2 should have some unspecified substitution applied to them) so perhaps you can elaborate with example input, example output, and what exactly you have tried so far.

OK, here is what I want to do

  1. I want perform some opreation on the lines which does not have search_pattern1 or search_pattern2.

  2. And the operation is - in those lines replace pattern1 by pattern2, if at all pattern1 is present. if pattern1 is not present leave them as it is.

In the case where I 've only one search_pattern, The following command works file -

sed '/search_pattern1/ !s/pattern1/pattern2/" file

Now, I want to add one more word to search "search_pattern2" with "or" condition, I'm not able to fit that into sed.

something like -

sed '/search_pattern1|search_pattern1/ !s/pattern1/pattern2/" file

should work,but it sed treats it like a single word - "search_pattern1|search_pattern1". it does not give a syntaxk error.

Did you try with a backslash before the |?

yes, I did..
with that too, it worked on all the data considering "'/search_pattern1\|search_pattern1/" as one word.

Can you try the following and let me know whether it works?

sed '/search_pattern1/ !s/pattern1/pattern2/;/search_pattern2/ !s/pattern1/pattern2/' file

You have the same search pattern on both sides of the vertical bar; is that intentional?

Does your local sed manual page have anything about alternation?

As a workaround, you can invert the condition:

sed -e '/search_pattern1/b' -e '/search_pattern2/b' -e 's/pattern1/pattern2/' file

Thants not intentional , two patterns are different.

Tiger,
Your methos doesn't work , It'll apple substitution on both search pattern lines which I want to exclude.

Let me give you real commands and data.
------------
$cat aa
------------
command:xxxdsjob} $ASYS_DSPROJ4 GWJCLS160PHS_HDRDTLPMT $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWJCLS185PHS_HDDTPMTPR $ASYS_GW_QUALIFIER
command:${rundsjob} $ASYS_DSPROJ4 GWMCLS230PHS_Hdr_Mapping $ASYS_GW_QUALIFIER
command: abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS_Hdr_SSD_Mapping $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWMCLS240PHS_Svc_Mapping $ASYS_GW_QUALIFIER
command: xfgsh.pl $ASYS_DSPROJ4 GWMCLS250PHS_Ub92_Mapping $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWTCLS070PHS_member_claim_step2_ft1007 $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWYALL090ALLpre_validate_delimited_file.1010 $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWYALL090ALLpre_validate_delimited_file.1011 $ASYS_GW_QUALIFIER
command:${Rundsjob} $ASYS_DSPROJ4 GWYALL100ALLvalidate_delimited_file.1010 $ASYS_GW_QUALIFIER
command:sendevent $ASYS_DSPROJ4 GWYALL100ALLvalidate_delimited_file.1010 $ASYS_GW_QUALIFIER
akfakfhfhs;kcvs;kcns;kcns;kcns;kcbns;kcvjs;kd
command: abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS_Hdr_SSD_Mapping $ASYS_GW_QUALIFIER

----------------------
sed "/rundsjob\|sendevent/ !s/\(command: *\)\(.*$\)/command: execute_args.sh '\2'/" aa
------------------------

    command: execute_args.sh 'xxxdsjob\} $ASYS_DSPROJ4 GWJCLS160PHS_HDRDTLPMT $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWJCLS185PHS_HDDTPMTPR $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWMCLS230PHS\_Hdr_Mapping $ASYS\_GW_QUALIFIER'
    command: execute_args.sh 'abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS\_Hdr\_SSD_Mapping $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWMCLS240PHS\_Svc_Mapping $ASYS\_GW_QUALIFIER'
    command: execute_args.sh 'xfgsh.pl $ASYS_DSPROJ4 GWMCLS250PHS\_Ub92_Mapping $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWTCLS070PHS\_member\_claim\_step2_ft1007 $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWYALL090ALLpre\_validate\_delimited_file.1010 $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{rundsjob\} $ASYS_DSPROJ4 GWYALL090ALLpre\_validate\_delimited_file.1011 $ASYS\_GW_QUALIFIER'
    command: execute_args.sh '$\{Rundsjob\} $ASYS_DSPROJ4 GWYALL100ALLvalidate\_delimited_file.1010 $ASYS\_GW_QUALIFIER'
    command: execute_args.sh 'sendevent $ASYS_DSPROJ4 GWYALL100ALLvalidate\_delimited_file.1010 $ASYS\_GW_QUALIFIER'
    akfakfhfhs;kcvs;kcns;kcns;kcns;kcbns;kcvjs;kd
    command: execute_args.sh 'abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS\_Hdr\_SSD_Mapping $ASYS\_GW_QUALIFIER'

-------------
But I want --
------------
command: execute_args.sh 'xxxdsjob} $ASYS_DSPROJ4 GWJCLS160PHS_HDRDTLPMT $ASYS_GW_QUALIFIER'
command: ${rundsjob} $ASYS_DSPROJ4 GWJCLS185PHS_HDDTPMTPR $ASYS_GW_QUALIFIER
command:${rundsjob} $ASYS_DSPROJ4 GWMCLS230PHS_Hdr_Mapping $ASYS_GW_QUALIFIER
command: execute_args.sh 'abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS_Hdr_SSD_Mapping $ASYS_GW_QUALIFIER'
command: ${rundsjob} $ASYS_DSPROJ4 GWMCLS240PHS_Svc_Mapping $ASYS_GW_QUALIFIER
command: execute_args.sh 'xfgsh.pl $ASYS_DSPROJ4 GWMCLS250PHS_Ub92_Mapping $ASYS_GW_QUALIFIER'
command: ${rundsjob} $ASYS_DSPROJ4 GWTCLS070PHS_member_claim_step2_ft1007 $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWYALL090ALLpre_validate_delimited_file.1010 $ASYS_GW_QUALIFIER
command: ${rundsjob} $ASYS_DSPROJ4 GWYALL090ALLpre_validate_delimited_file.1011 $ASYS_GW_QUALIFIER
command: execute_args.sh '${Rundsjob} $ASYS_DSPROJ4 GWYALL100ALLvalidate_delimited_file.1010 $ASYS_GW_QUALIFIER'
command:sendevent $ASYS_DSPROJ4 GWYALL100ALLvalidate_delimited_file.1010 $ASYS_GW_QUALIFIER
akfakfhfhs;kcvs;kcns;kcns;kcns;kcbns;kcvjs;kd
command: execute_args.sh 'abcd.sh $ASYS_DSPROJ4 GWMCLS235PHS_Hdr_SSD_Mapping $ASYS_GW_QUALIFIER'

I've got a workaround, But thats not fool-proof -

sed "/[rs][ue]nd[se][jv][oe][bn]t*/ !s/\(command: *\)\(.*$\)/command: execute_args.sh '\2'/" aa

Did the sed b workaround I posted above not work for you?

I just tried.. It worked !!!

Thank you very much....

previuosly I did it with !s. there it didn't work.