2015-07-12 20 views
0

我使用下面的命令重定向的sed将一个tmp文件的输出:重定向sed的输出(来自grep的管道)到一个文件中的C程序

grep --include=*.txt -A 3 -rnw abx/ -F -e 'simple' | sed -n 's#.*/\([^/]*\.txt\).*"\([^"]*\)*"[[:space:]]*,[[:space:]]*/\*[[:space:]]*col[[:space:]]*\*/#\1\n\2#p' > tmp 

,如果它的输出重定向tmp中在终端中使用,但在C程序中,没有任何内容写入'tmp'。

C程序:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() { 
    char cmd[1028]; 

    strcpy (cmd, "grep --include=*.txt -A 3 -rnw abx/ -F -e 'simple' | sed -u -n 's#.*/\\([^/]*\\.txt\\).*\"\\([^\"]*\\)*\"[[:space:]]*,[[:space:]]*/\\*[[:space:]]*col[[:space:]]*\\*/#\\1\\n\\2#p' > tmp"); 

    system (cmd); 
    return 0; 
} 

编辑: 实施例文本文件:

simple = 
"sik", /* fill */ 
"trauma", /* type */ 
"hui", /* col */ 

此文件存储在ABX /。

+3

你可以打印字符串,看看你在做什么。您也可以检查系统调用的返回值。 – mch

+0

系统调用返回值为0.我编辑了这个问题。 – Shahzad

+0

可能重复的[重定向输出到C文件](http://stackoverflow.com/questions/8516823/redirecting-output-to-a-file-in-c) – Cyrus

回答

0

您可能需要在正则表达式中“双重转义”“\”。

([^/]*\\.txt\\) --> ([^/*\\\\.txt\\\\) 

当它建立的字符串,我想CMD也将改变“\ \”,以“\” C会改变“\ \”到“\”。

+0

它们已经被转义,因为它们在字符串中。 – Shahzad

相关问题