ifdef在c语言中的选择性预处理

ifdef在c语言中的选择性预处理,c,c-preprocessor,preprocessor-directive,C,C Preprocessor,Preprocessor Directive,我有大的.c和.h文件,我必须从中只选择满足以下条件的预处理器指令 如果值==5 | |值==6 其余的预处理器指令应该保持不变。我已经研究了unifdef和sunifdef,它们将允许我识别if和ifdef,但我希望有选择地选择不是所有if和ifdef。是否有一个实用程序允许我给出,例如,VALUE==5并只选择那些 基本上,我需要根据输入值选择预处理器指令。尝试运行脚本,它将打印代码行和if块中的代码。 这只会静态扫描文件。你可以试试看 # ./run.sh filename "String

我有大的.c和.h文件,我必须从中只选择满足以下条件的预处理器指令

如果值==5 | |值==6

其余的预处理器指令应该保持不变。我已经研究了unifdef和sunifdef,它们将允许我识别if和ifdef,但我希望有选择地选择不是所有if和ifdef。是否有一个实用程序允许我给出,例如,VALUE==5并只选择那些


基本上,我需要根据输入值选择预处理器指令。

尝试运行脚本,它将打印代码行和if块中的代码。 这只会静态扫描文件。你可以试试看

# ./run.sh filename "String to search in if"
# example ./run.sh test.h "#if VALUE"

# Get the line number of Searching string locations
# store it in a file
awk "/$2/{ print NR }" $1 > temp_if

# to store total lines present inside the if contruct
totalLinesCount=0

# read one line at a time, that is go through one occurance of string at a time
while read matchLineNum
do
    printf "Match Found in line number $matchLineNum\n"
    #search for first occurance of #elif from the matched string.
    elifLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#elif/{ print NR;exit }")
    if [ "$elifLineNum" == "" ]
    then
        # if not found set it to some high value
        elifLineNum=$(wc -c $1)
    fi
    elseLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#else/{ print NR;exit }")
    if [ "$elseLineNum" == "" ]
    then
        elseLineNum=$(wc -c $1)
    fi
    endifLineNum=$(tail -n +$(expr $matchLineNum + 1) $1 | awk "/#endif/{ print NR;exit }")

    # check if #endif / #elif / #else occurs first
    if [ $endifLineNum -lt $elseLineNum -a $endifLineNum -lt $elifLineNum ]
    then
        # store the code count
        totalLinesCount=$(expr $totalLinesCount + $(expr $endifLineNum - 1))
        # store the code
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $endifLineNum - 1) >> out

    elif [ $elseLineNum -lt $endifLineNum -a $elseLineNum -lt $elifLineNum ]
    then
        totalLinesCount=$(expr $totalLinesCount + $(expr $elseLineNum - 1))
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $elseLineNum - 1) >> out

    elif [ $elifLineNum -lt $elseLineNum -a $elifLineNum -lt $endifLineNum ]
    then
        totalLinesCount=$(expr $totalLinesCount + $(expr $elifLineNum - 1))
        tail -n +$(expr $matchLineNum + 1) $1 | head -n $(expr $elifLineNum - 1) >> out

    else
        printf "\n Error \n"
    fi
done < temp_if
printf "\nTotal number of lines present inside the given condition is $totalLinesCount\n"
rm -f temp_if
printf '\nFinal Extracted Out\n'
cat out
rm -f out

你想做什么?删除此指令?得到他们之间的密码?只需打印包含它们的行?获取行号?基本上我需要识别它们,甚至获取它们之间的代码行。这个答案可能会解决您的问题/usr/bin/run.sh:line 4:$'\r':command not found/usr/bin/run.sh:line 7:$'\r':command not found/usr/bin/run.sh:line 34:意外标记附近的语法错误elif'/usr/bin/run.sh:run.sh:line 34:elif[$Elselinum-lt$endifLineNum-a'Elselinum-lt$Eliflinum]。你能帮我一下吗。。我在windows环境中使用cygwin来运行这个脚本使用dos2unix-ascii run.sh test.h,要将windows格式转换为unix格式,实际上windows和unix在文件中用不同的方式表示换行符。转换文件运行脚本后,我使用了您建议的命令,我还可以通过查看执行该命令后文件的最后修改日期来确认某些内容正在更改。但我还是犯了同样的错误。。知道原因是什么吗?谢谢大家的帮助..我已经编写了自己的实用程序来实现这一点,因为现有的解决方案都没有帮助我。