Replacement of strings containing Linux spaces

I wrote a code that ** rewrites the character string with a shell script and fills in the missing parts with spaces **, so I will keep a memorandum of what I checked and where I stumbled.

Task

background

A story during C language training. When writing the source code, it was necessary to rewrite the header comment every time. For example

training1-1.c


/*###################################################*/
/*#           File      :  training1-1.c            #*/
/*#           Date      :  2019/10/22               #*/
/*###################################################*/

int main(void){
    printf("Hello World\n");
    return 0;
}

Like, Kanji. Especially during training, the file name is almost the same as "training1-1.c", so if you repeat file creation → file name renaming, it will be troublesome to retype it every time.

What you want to achieve

  1. ** Automatically edit header comments and align the number of comment-out spaces neatly **
  2. ** If you write the source code with vim and save it, it will output the execution result as it is **

Since I was doing everything from file creation to compilation in a Linux environment, I made it using my knowledge of shell scripts that I was just studying.

Deliverables

The shell script (zikko.sh) ** that edits, compiles, and executes the ** header comments created this time will be used together with the underlying template (template.c). The execution method and the contents of zikko.sh and template.c are as follows. Also, can be before the extension.

.sh


#Execution method
$ ./zikko.sh template <file name>

zikko.sh


#!/bin/bash

template=$1
newFile=$2
date=$(date "+%Y/%m%d")
convertSymbolFile=$"FILE_NAME"
convertSymbolDate=$"YYYY/MM/DD"

#Edit the header file if the file does not exist
if [ ! -e $newFile.c ]; then
    #Adjust the space to change the title of the file
    if [ ${#newFile} -le ${#convertSymbolFile} ]; then
        diffOfStr=$((${#convertSymbolFile}-${#newFile}))
        numOfSpace=$(seq -s" " $((diffOfNum+1)) | tr -d '[:digit:]')
        newFileTitle=$newFile$numOfSpace
        sed -e "s:$convertSymbolFile:$newFileTitle:g" $template.c > $newFile.c
    #else
    #The length of the converted string> FILE_I tried to write the processing for NAME, but I gave up because of time.
    #I feel that there is no problem if the character string of convertSymbolFile is lengthened.
    fi
    #Change date
    sed -i -e "s:$convertSymbolDate:$date:g" $newFile.c
fi

#Start editing from the line where the main function is written
mainFuncLow=$(grep SOURCE_CODE -n $newFile.c | cut -d ":" -f 1)
vim -c $mainFuncLow $newFile.c

#Compile and run
gcc -o $newFile.x $newFile.c
./$newFile.x

template.c


/*###################################################*/
/*#           File      :  FILE_NAME                #*/
/*#           Date      :  YYYY/MM/DD               #*/
/*###################################################*/

int main(void){
    //SOURCE_CODE
    return 0;
}

If you add the character string before conversion to the template and play with the process of changing the title of the zikko.sh file a little, you should be able to handle the addition of a simple header comment ... I feel that the code is long for the work that seems simple. Please let me know if there is a better way.

Stumble point

Repeating spaces

When I searched for "Linux string replacement, repeat", I found quite a few articles about repeating symbols, but I couldn't find many articles that repeatedly insert spaces. The easiest method I found was to send the result of the seq command to the tr command in the pipeline [^ 1].

Stumble point 1.sh


#diffOfNum=4

$ seq -s" " $((diffOfNum+1)) | tr -d '[:digit:]'
#->***** Instead of space*Display with

--Note that even if NUM (arbitrary value) is given as an argument of the print command, it will be displayed only (NUM-1) times on the screen. ** If you want to display NUM times, you need to give (NUM + 1) to the argument . - If you want to display spaces repeatedly, you need to put "" after the -s option **. For symbols, enter the symbol you want to display repeatedly after the -s option.

String concatenation

To combine strings with a shell script, it seems that shell variables should be written consecutively [^ 2].

Stumble point 2.sh


#newFile=file1 ※<file name>
#numOfSpace=****

$ newFileTitle=$newFile$numOfSpace
#->file1****

How to use the sed command

I often used the sed command to replace strings, so I'll briefly note how to use it and what I stumbled upon. First, the execution method is as follows.

$ sed [option] <script> <file name>

Recommended Posts

Replacement of strings containing Linux spaces
Bulk replacement of strings in Python arrays
00. Reverse order of strings
Concatenation of ruby strings
Meaning of Linux permissions
[Linux] Installation of nvm
Addition of amazon linux swap
Incremental backup of Linux (restore)
Summary of Linux distribution types
Japanese translation of Linux manual
Linux: DNS replacement in systemd.
A brief summary of Linux
Meaning of Linux directory permissions