I need to update my cpp source files header by adding extra one space before starting of the below lines in header information -
This file is an example header file.
There are 100 files are present with this formats.
Some files are with proper space before starting of each line in this paragraph.
How to move each line in this paragraph with single space at start using any script.
All above lines are fixed throughout all files.
Present Header format:
/* FileHeaderBegin
ALL INFORMATION ARE BELONGS TO ABCDEF
Copyright 2015 - 2020 ABCDEF LTD
All Rights Reserved
This file is an example header file.
There are 100 files are present with this formats.
Some files are with proper space before starting of each line in this paragraph.
How to move each line in this paragraph with single space at start using any script.
All above lines are fixed throughout all files.
*
*
* FILENAME: test.cpp
*
* DESCRIPTION: Sample file for command test
*
* FileHeaderEnd */
Expected:
/* FileHeaderBegin
ALL INFORMATION ARE BELONGS TO ABCDEF
Copyright 2015 - 2020 ABCDEF LTD
All Rights Reserved
This file is an example header file.
There are 100 files are present with this formats.
Some files are with proper space before starting of each line in this paragraph.
How to move each line in this paragraph with single space at start using any script.
All above lines are fixed throughout all files.
*
*
* FILENAME: test.cpp
*
* DESCRIPTION: Sample file for command test
*
* FileHeaderEnd */
Query: Is there any command or script available to do that changes. I have total 270 files with few files with proper space added and majority files with no space added.
CodePudding user response:
This might work for you (GNU sed):
sed -i '/FileHeaderBegin/{:a;n;s/^\S/ &/;/FileHeaderEnd/!ba}' file1 ... filen
Turn on edit inplace option -i
Insert a space at the front of any line that begins with a non-space character between the lines containing FileHeaderBegin
and FileHeaderEnd
.
Alternative solution:
sed -i '/FileHeaderBegin/,/FileHeaderEnd/s/^\S/ &/' file1 ... filen
CodePudding user response:
If your file header always looks like that you can do the following with GNU awk:
awk '(FNR==1){h=0}(!h && !/^ /){$0=" " $0}/*\//{h }1' file
You can now put this in a loop to process your files.
This awk script searches for the string "*/" which appears for the first time right after the header. Only until this string is found, we check for lines that mis the space at the beginning.
For inplace modifications with awk: see Save modifications in place with awk