Delete a line in a text file using c program/perl script + system call

Hi,
I need to execute a perl script from a c program. so I presume I have to use System call. I need to pass 2 arguments to the perl script.
Arg1: filename
Arg2: string

The script should search & delete  the line with the matching string.
It should return appropriate retVal back to the System code so that
the c program can check against it.

A functioning code snippet will be helpful since I am not much
fimiliar with perl.

Also is there any way to do this in C without creating a new file.

Thanks in advance for help and feedback.
LVL 4
sanjay_thakurAsked:
Who is Participating?
 
ozoCommented:
If you use a an alphanumeric character for #
you also need a space
 sprintf(cmd,"perl -i -pe '$ret+=s Z.*\\Q%s\\E.*ZZs;END{exit $ret}' %s", string, filename);

If "DiscoveryAddress=10.17.235.127:3260" is the filename
then there also seems to be a space missing before the last %s
0
 
FishMongerCommented:
I didn't test this so it may need a minor tweek.

#!/usr/bin/perl -w

$file = $ARGV[0];
$string = $ARGV[1];

open F, $file or die $!;
unlink $file if ( grep(/\Q$string/, <F>) );
0
 
ozoCommented:
int ret;
char *cmd = (char *)malloc(strlen(filename)+strlen(string)+40);
sprintf(cmd,"perl -i -pe '$ret+=s/.*\\Q%s\\E.*//s;END{exit $ret}' %s",string,filename);
ret = system(cmd);
printf("%d lines deleted\n",ret>>8);
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
FishMongerCommented:
Oops, I was deleting the file not the string in the file.  Go with ozo's solution.
0
 
sanjay_thakurAuthor Commented:
HI ozo,
I am getting this error

perl cmd = perl -i -pe'$ret+=s/.*\Q/etc/vmkiscsi.conf\E.*//s;END{exit $ret}'DiscoveryAddress=10.17.235.127:3260
Bareword found where operator expected at -e line 1, near "s/.*\Q/etc/vmkiscsi"
Backslash found where operator expected at -e line 1, near "conf\"
syntax error at -e line 1, near "s/.*\Q/etc/vmkiscsi"
Substitution pattern not terminated at -e line 1.


0
 
ozoCommented:
sprintf(cmd,"perl -i -pe '$ret+=s#.*\\Q%s\\E.*##s;END{exit $ret}' %s", string, filename);
where # is a character not contained in string
0
 
sanjay_thakurAuthor Commented:
I replaced # with Z since this is not in the string

This is what I get


perl cmd = perl -i -pe'$ret+=sZ.*\Q/etc/vmkiscsi.conf\E.*ZZs;END{exit $ret}'DiscoveryAddress=10.17.235.127:3260
Bareword found where operator expected at -e line 1, near "*\Q"
        (Missing operator before Q?)
Backslash found where operator expected at -e line 1, near "conf\"
syntax error at -e line 1, near "*\Q"
syntax error at -e line 1, near "10.17.235.127:"
Execution of -e aborted due to compilation errors.


Was I not supposed to replace all the #?


0
 
sanjay_thakurAuthor Commented:
Thank you for your prompt help and patience and the solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.