2

I'm trying to delete some special lines in a log file, so I use sed of busybox on an embeded linux system.

# sed
BusyBox v1.18.4 (2013-01-16 16:00:18 CST) multi-call binary.

Usage: sed [-efinr] SED_CMD [FILE]...

Options:
        -e CMD  Add CMD to sed commands to be executed
        -f FILE Add FILE contents to sed commands to be executed
        -i      Edit files in-place (else sends result to stdout)
        -n      Suppress automatic printing of pattern space
        -r      Use extended regex syntax

If no -e or -f, the first non-option argument is the sed command string.
Remaining arguments are input files (stdin if none).
  1. execute the following command under shell and everything works fine:

    export MODULE=sshd
    sed "/$MODULE\[/d" logfile
    
  2. but if I try to use the following C code to accomplish this:

    char logfile[] = "logfile";
    char module_str[] = "sshd";
    char env_str[64] = {0};
    int offset = 0;
    
    strcpy(env_str, "MODULE=");
    offset += strlen("MODULE=");
    strcpy(env_str + offset, module_str);
    putenv(env_str);
    system("sed \"/$MODULE\[/d\" logfile");
    

when executing the a.out, I got the error message:

sed: unmatched '/'

what's wrong with my 'system()' call? I'm totally a newbie in text processing, so anybody can give me some clue? Thanks.

Best regards, dejunl

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
dejunl
  • 57
  • 1
  • 8
  • do you need to use `strcat` or `+` instead of including $MODULE? – PeppyHeppy Feb 03 '13 at 09:28
  • I want to use an env variable in the sed command, using 'strcat' can solve this? – dejunl Feb 03 '13 at 09:44
  • 1
    Why are you wrapping your sed command in a C program? – Ed Morton Feb 03 '13 at 14:53
  • Be careful with `putenv()`; it uses the (local) variable you provide in the environment, which tends to lead to trouble. Make sure the variable has a long enough lifetime (`static char env_str[64] = "";`) or use `setenv()` instead (`setenv("MODULE", module_str, 1);`). – Jonathan Leffler Feb 03 '13 at 17:02
  • 1. To Ed Morton,the above code is just a snippet of my whole application written in C, I'm implementing a module dealing with a logfile on an embeded system. PS, thanks for your nice formatting. – dejunl Feb 04 '13 at 00:45
  • 2. To Jonathan Leffler, I've searched the differences between 'putenv' and 'setenv', but I don't know which one to choose. See the following links: [http://www.greenend.org.uk/rjk/tech/putenv.html] and [http://stackoverflow.com/questions/5873029/questions-about-putenv-and-setenv], especially the latter one. – dejunl Feb 04 '13 at 00:48

1 Answers1

2

straight off I can see that the \ before the [ is going to be swallowed by 'C'

so you'll need to double it,

system("sed \"/$MODULE\\[/d\" logfile");

But the shell might want to swallow the one that's left swallow that one so double it again

system("sed \"/$MODULE\\\\[/d\" logfile");

of course system("sed \"/$MODULE\\[/d\" logfile"); can't be sure I'm reading the question you posed. try it with echo instead of sed and adjust it until the string comes out as you want sed to see it.

Jasen
  • 11,837
  • 2
  • 30
  • 48
  • Oh, god, my system crashed, I can't boot it now, I will try your way later. Thanks for your reply. @Jasen – dejunl Feb 03 '13 at 10:09
  • I had the same problem in R (command worked in terminal but not when called by `system()`) and this solved it for me!!! Thanks! – Antoine Feb 17 '21 at 10:45