Re: sed, awk, or something...help meeeeeeeeeee

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 28.09.2009 21:07, schrieb Sharpe, Sam J:
2009/9/28 Kanwar Ranbir Sandhu<m3freak@xxxxxxxxxxxxxxxxxx>:
On Mon, 2009-09-28 at 19:33 +0100, Sharpe, Sam J wrote:
Yes, but it involves reading the man page for sed - I'm not totally
sure you are capable of doing that.

I suggest looking at the output of "man sed", finding the section
about the command "r" and then reading the next few lines.
Oh my word.  I can't believe I missed that.  So, I've solved my little
problem with this:

sed '/BB/R file2' file1

Sweet!
That alone doesn't fit your original specification:

[sam@samlap Desktop]$ sed -e '/BB/R file2' file1
AA
BB
BBBB1
CC
DD

AA
BB
BBBB2
CC
DD

You specified that BB was to be /replaced/ with BBBB1, so you will
need to do this:

[sam@samlap Desktop]$ sed -e '/BB/R file2' -e '/BB/d' file1
AA
BBBB1
CC
DD

AA
BBBB2
CC
DD

This code may fail in case file 2 contains a line with BB only,
in that case the replacement will be removed too. Enclosed
you will find my suggestion for a C program (still unchecked).

Joerg
#include <stdlib.h>
#define GNU_SOURCE
#include <stdio.h>
#include <string.h>

#define gettext(file)\
  getline(&line,0,file);\
  if(i=strlen(line)-1,line[i]=='\n')line[i]=(char)0;

int main(int argc,char **argv){
  FILE *file1,*file2,*tempfile;
  char *line=NULL;
  int i;
  if(argc<3)printf("usage: replace file1 file2\n");
  file1=fopen(argv[1],"r");
  file2=fopen(argv[2],"r");
  tempfile=fopen("tempfile","w");
  while(!feof(file1)){
    gettext(file1);
    if(!strcmp(line,"BB")){
      free(line);line=NULL;
      if(feof(file2)){
        printf("unexpexted EOF in %s\n",argv[2]);
        return 2;
      }
      gettext(file2);
    }
    fputs(line,tempfile);
    putc('\n',tempfile);
    free(line);line=NULL;
  }
  fclose(file1);
  fclose(file2);
  fclose(tempfile);
  remove(argv[1]);
  rename("tempfile",argv[1]);
  return 0;
}

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux