8

i have the below code to replace some strigns in multiple files, but the for loop is checking for the first file and not executing the perl script. below is my code

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in $(./zebu.work.post_opt/ZEBU_CTO_FT_MOD*);
    do
    perl -i -p -e 's/input/inout/g' $file; 
        perl -i -p -e 's/output/inout/g' $file;
        perl -i -p -e 's/wire.*\n/tran\(i0,\ o\);/g' $file;
        perl -i -p -e 's/assign.*\n//g' $file;
    done
fi
  • 2
    Remove the $(...) around your filename globbing pattern. You don't need a command substitution there. You may also combine the four Perl invocations to a single one for speed. – Kusalananda Aug 22 at 15:43
10

The $(foo) construct will run the command foo and replace $(foo) with the output of running foo. You want a glob, that's not a command. What you're doing is attempting to run all files called ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*. All you need is:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*;
    do
        perl -i -p -e 's/input/inout/g' "$file"
        perl -i -p -e 's/output/inout/g' "$file"
        perl -i -p -e 's/wire.*\n/tran\(i0,\ o\);/g' "$file"
        perl -i -p -e 's/assign.*\n//g' "$file"
    done
fi

Or, more simply:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    for file in ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*;
    do
        perl -i -p -e 's/input/inout/g; s/output/inout/g; 
                       s/wire.*\n/tran\(i0,\ o\);/g; 
                       s/assign.*\n//g' "$file"
    done
fi

Or even more simply:

if [ -f zebu.work.post_opt/ZEBU_CTO_FT_MOD.v ]
then
    perl -i -p -e 's/input/inout/g; s/output/inout/g; 
                   s/wire.*\n/tran\(i0,\ o\);/g; 
                   s/assign.*\n//g' ./zebu.work.post_opt/ZEBU_CTO_FT_MOD*
fi
  • 3
    Is the loop even needed? Can perl -ip work on several files at once? I've never tested taht. – Kusalananda Aug 22 at 15:45
  • @Kusalananda indeed it can. Should have thought of that, thanks! – terdon Aug 22 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.