Thread: Script to add a fixed header to multiple .c/.h files

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    19

    Script to add a fixed header to multiple .c/.h files

    I would like to update all the files (.c / .h) of my project, using a script (for Linux), to add a fixed header of the following type:
    Code:
    /*
     * <File Name>
     * <Author>:                 <Date>:
     * 
     * Description:
     *
     * History:
     *
    */
    How can I do ?

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Something like this maybe:

    Code:
    cat $1 $2 > .tmp && mv .tmp $2
    Running the script:

    Code:
    prepend preamble.txt file.txt

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    Yes, thank:
    Code:
    #!/bin/bash
    for file in *.c; do
        cat $1 $file > .tmp && mv .tmp $file
    done

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    Now it is a matter of reading the file name and inserting it in the header, then inserting the name of the author and reading the date of creation of the file, always inserting everything in the header. How you do it ?

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    At the moment I wrote this:
    Code:
    #!/bin/bash
    for file in *.c; do
        echo "/*" > intestazione.txt
        echo " * File Name: " $file >> intestazione.txt
        echo " *" >> intestazione.txt
        echo " * Author: Giorgio Guglielmone    Date: " $date >> intestazione.txt
        echo " *" >> intestazione.txt
        echo " * Description:" >> intestazione.txt
        echo " *" >> intestazione.txt
        echo " * History:" >> intestazione.txt
        echo " *" >> intestazione.txt
        echo "*/" >> intestazione.txt
        cat intestazione.txt $file > .tmp && mv .tmp $file
    done
    instead of $ date I need to be able to use the bash date command to get the current bash script launch date. How should I write it ?

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Code:
    author='Your name here'
    for file in *.c; do
      sed -i "s/<File Name>/$file/;s/<Author>/$author/;s/<Date>/$(date)/" $file
    done

  7. #7
    Registered User
    Join Date
    Apr 2021
    Posts
    19
    Code:
    echo " * Author: Giorgio Guglielmone    Date: " `date` >> intestazione.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reason behind multiple levels of header files?
    By Leam in forum C Programming
    Replies: 6
    Last Post: 01-24-2016, 10:10 AM
  2. Replies: 11
    Last Post: 08-29-2011, 01:35 PM
  3. Program or Script to Add Header to .c and .h files.
    By Sink0 in forum C Programming
    Replies: 15
    Last Post: 01-11-2011, 08:08 PM
  4. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  5. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM

Tags for this Thread