1

I have large .c and .h files from which i have to select only preprocessors directives satisfying these conditions as below

#if(VALUE==5) ||(VALUE==6)

Rest of preprocessors directives should remain untouched.I have already looked into unifdef and sunifdef they will allow me to identify #if and #ifdef but i want to chose selectively not all #if, #ifdefs. Is there a utility thats allows me to give, e.g., VALUE==5 and chose only those?

Basically I need preprocessor directives selection based on my input value.

3
  • what do you want to do? remove this directives? get the code between them? just print the lines containing them? get the line numbers? Commented Feb 4, 2015 at 15:54
  • Basically i need to identify them and even get the lines of code between them. Commented Feb 4, 2015 at 16:07
  • This answer stackoverflow.com/a/4092448/700170 may solve your problem Commented Feb 4, 2015 at 20:44

1 Answer 1

1

Try Running the Script, it will print the lines of code and code present inside the #if block. This only scans the file statically. You can give it a try.

# ./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
Sign up to request clarification or add additional context in comments.

4 Comments

/usr/bin/run.sh: line 4: $'\r': command not found /usr/bin/run.sh: line 7: $'\r': command not found /usr/bin/run.sh: run.sh: line 34: syntax error near unexpected token elif' /usr/bin/run.sh: run.sh: line 34: elif [ $elseLineNum -lt $endifLineNum -a 'elseLineNum -lt $elifLineNum ]. Can you please help me with it.. I am in windows environment using cygwin to run this script
use dos2unix -ascii run.sh test.h, to convert the windows format to unix format, actually windows and unix have a different way of representing newline character in file. After converting the files run the script
I used the command you suggested and i can also confirm something is changing by seeing the last modified date of file after execution of that command. But still i am getting same error.. Any idea what could be the reason?
Thanks for all help..I have written my own utility to do this since none of the existing solutions helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.