Skip to main content
added 6 characters in body
Source Link
stdcerr
  • 2.1k
  • 14
  • 49
  • 75

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I use grep instead of find because the file names are unknown but I'm looking at the file contents instead.

Contents of sed.c:

$ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv));

 $ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv)); 

i.e. I want to apply the two sed replacements only to lines with printf, %s and bcm_errstr(rv) in them.

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I use grep instead of find because the file names are unknown but I'm looking at the file contents instead.

Contents of sed.c:

$ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv));

i.e. I want to apply the two sed replacements only to lines with printf, %s and bcm_errstr(rv) in them.

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I use grep instead of find because the file names are unknown but I'm looking at the file contents instead.

Contents of sed.c:

 $ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv)); 

i.e. I want to apply the two sed replacements only to lines with printf, %s and bcm_errstr(rv) in them.

added 114 characters in body
Source Link
stdcerr
  • 2.1k
  • 14
  • 49
  • 75

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I use grep instead of find because the file names are unknown but I'm looking at the file contents instead.

Contents of sed.c:

$ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv));

i.e. I want to apply the two sed replacements only to lines with printf, %s and bcm_errstr(rv) in them.

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I would like to scan a whole file tree and do two replacements any line with two matches, i.e.:

printf("Hello WORLD! %s, %d\n",bcm_errstr(rv),var); dprintf("kjhgjkhfkhgfjgd %s\n",bcm_errstr(rv)); 

should become

printf("Hello WORLD! %d, %d\n",rv,var); dprintf("kjhgjkhfkhgfjgd %d\n"rv); 

I tried the following without success (sed.c being my test file containing two lines that will match the query): p$ grep printf | grep "%s" | grep -rl bcm_errmsg\(rv\) sed.c | xargs sed -i -e 's/%s/%d/' -e 's/bcm_errstr\(rv\)/rv/'

I use grep instead of find because the file names are unknown but I'm looking at the file contents instead.

Contents of sed.c:

$ cat sed.c printf("kjhlkjhlkjh%dkjhgljhglj\n",bcm_errmsg(rv)); dprintf("HELLO WORLD %d %d\n",test,bcm_errmsg(rv));

i.e. I want to apply the two sed replacements only to lines with printf, %s and bcm_errstr(rv) in them.

edited tags; edited title
Link
Jeff Schaller
  • 68.9k
  • 35
  • 122
  • 268

multiple replacementreplacements on single limeline with sed

Source Link
stdcerr
  • 2.1k
  • 14
  • 49
  • 75
Loading