简体   繁体   中英

Commenting out lines with a specific substring across multiple files in Sublime/Linux

I have a bunch of files that contain lines that I want to comment out containing a specific string.

In the following example, I'd need to comment out only the lines with substring Sensor . I'm using Sublime editor so doing through that would be amazing but if there's a possibility with some linux commands, that would work too.

//fileA.c
printf ("Sensor reading A");  // need to comment out this
// ...

// fileB.C
printf ("Function starts...");
printf ("Sensor A value");   // need to comment out this
// ...

In shell:

for file in *.c; do # Adjust as needed
    printf '%s\n' 'g/Sensor/s!^!//!' w | ed -s "$file"
done

For each file, open it with the standard POSIX file editor ed , and add // to the beginning of any line containing Sensor , then save the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM