Home BASH code snippets
Post
Cancel

BASH code snippets

BASH Code Snippets

Some small and simple code snippets from me.

Bash script to replace a string in a file with the filename

This was usefull for me to replace some header comments for .h / .cpp files when I used copy paste to insert a standart header to the sourcfiles and then wanted to adjust the text reflecting the filename.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#==========================================================
# replaceHeader.sh 
#
# Author: 	DaVe inc. Kim David Hauser (kimhauser.ch)
# Date: 	2023-04-27 19:32:59
#
# Description:
# Replace a variable file content string with its filename
#==========================================================

for f in $(find . -name '*.h'); 
do 
	search="Dbg.h"
	full="$f"
	basename "$f"
	n="$(basename -- $f)"
	sed -i '' -e "s/  \\"$search"/  \\"$n"/g" "$full"
done

for f in $(find . -name '*.cpp'); 
do 
	search="Dbg.cpp"
	full="$f"
	basename "$f"
	n="$(basename -- $f)"
	sed -i '' -e "s/  \\"$search"/  \\"$n"/g" "$full"
done

Description of the script

In this example the script searches for all “.h” and “.cpp” files in a directory (recursively) and then replaces the file content string “ Dbg.h” or “ Dbg.cpp” with its actual filename (only filename no path).

How to use the script

Paste the code into a “replaceHeader.sh” file, and adjust the nessesary variables (search, ‘.h’ and ‘.cpp’). Then make it executable with console command “chmod +x replaceHeader.sh” and then execute it in the parent directory of the location where the files you want to amend are placed in.

Download script

Download replaceHeader.sh script

This post is licensed under CC BY 4.0 by the author.