Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-04-13
8
Medium Priority
?
2,579 Views
Last Modified: 2008-03-03
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.
0
Comment
Question by:sanjay_thakur
  • 3
  • 3
  • 2
8 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 13777567
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
 
LVL 85

Expert Comment

by:ozo
ID: 13777590
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 13777600
Oops, I was deleting the file not the string in the file.  Go with ozo's solution.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:sanjay_thakur
ID: 13778061
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
 
LVL 85

Expert Comment

by:ozo
ID: 13778100
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
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 13778233
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
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 13778279
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
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 13778651
Thank you for your prompt help and patience and the solution.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question