Home > Software design >  How to edit multiple lines in sed
How to edit multiple lines in sed

Time:06-06

I have 45 lines of code that need a sed command. Due to the recent change in GNU all my scripts are breaking and need -std=legacy & -fallow-invalid-boz. The only way I know how to do this is with sed. I'm not a computer programmer and sed is simple and easy to understand.

These are a sample of my sed commands.

enter image description here

Is there a way to do all these sed commands in a loop or with sed itself. If there is another editor that makes it easier I can try to learn that too.

I have tried this

for X in [24,28,32,36,40,45,49,53,56,60,64,68,69,73,74,79]
    sed -i '$Xs/= /= -std=legacy -fallow-invalid-boz /g' $HOME/WRF/Downloads/NCEPlibs/macros.make.linux.gnu
done

But I get the error:

$ for X in [24,28,32,36,40,45,49,53,56,60,64,68,69,73,74,79] sed -i '$Xs/= /= -std=legacy -fallow-invalid-boz /g' $HOME/WRF/Downloads/NCEPlibs/macros.make.linux.gnu done bash: syntax error near unexpected token sed' bash: syntax error near unexpected token done'

CodePudding user response:

doing it like this is a lot smarter

y="24 28 32 36 40 45 49 53 56 60 64 68 69 73 74 79"
for X in $y; do
  sed -i "${X}s/= /= -std=legacy -fallow-invalid-boz /g" $HOME/WRF/Downloads/NCEPlibs/macros.make.linux.gnu
done

First of all, you forgot the do statement in for so the for statement will just fail before it can even execute.

Second of all [24,28,32,36,40,45,49,53,56,60,64,68,69,73,74,79] in not valid as for uses newlines and or white spaces to declare a new value going from left to right.

And last but not least, using $X is not valid in this example as bash reads it as $Xs/ so using ${X} is the correct way and of course using "" instead of using '' so ${X} can actually be used.

  • Related