Saturday, April 27, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 3569  / 2 Years ago, wed, august 17, 2022, 5:49:26

Im using read in a while loop to automatically change a makefile after download.



Here is part of the script,



    while read a; do
if [[ "$a" = "FCOMPL=g77" ]]
then echo "FCOMPL=gfortran" >> makefile
elif [[ "$a" = "FFLAGC=-Wall -O" ]]
then echo "FFLAGC=-Wall -O -fno-backslash" >> makefile
else
echo $a >> makefile
fi
done <makefile.orig


The problem is that I lose the tabulation.



Any idea of how I can avoid that?


More From » bash

 Answers
7

Instead of using bash to do the task, you could learn sed:



sed -e 's/^FCOMPL=g77$/FCOMPL=gfortran/' 
-e '/^FFLAGC=-Wall -O$/s/$/ -fno-backslash/' makefile.orig > makefile


Each -e gives sed a command to perform. In this case (1st -e), the s command performs a substitution: s/foo/bar/ replaces the 1st occurrence of foo on each line by bar. To be sure we work on complete line, I added ^ (beginning of the line) and $ (end of the line).



You can prefix a command by a selector. In this case (2nd -e), the s command is applied only to a line matching ^FFLAGC=-Wall -O$.



You could even use the -i flag to replace the file:



sed -i -e 's/^FCOMPL=g77$/FCOMPL=gfortran/' 
-e '/^FFLAGC=-Wall -O$/s/$/ -fno-backslash/' makefile

[#32231] Thursday, August 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
istmasted

Total Points: 287
Total Questions: 130
Total Answers: 153

Location: Burkina Faso
Member since Thu, Dec 23, 2021
2 Years ago
istmasted questions
;