command line - Sed bash script iterating multiple times - Ask Ubuntu


i have assignment of editing text file, called lololol.

hrfjgekgjhejgkjvnejgkjeg bob bo b  gary gary gary gary  fog gary bob bob bob gary gary  the bird in the bush!  lol mate! 

i have bash script file called lolz.script:

#!/bin/bash  sed '1i hee hee hee\n ho ho ho\n' $1 sed 's/bob/bob/g' $1 

running .\lolz.script lololol @ command line, multiple iterations of lololol text- precisely, 1 iteration per sed command.

for example, output shows first , second sed commands (applied each) here:

*hee hee hee  ho ho ho* hrfjgekgjhejgkjvnejgkjeg bob bo b  gary gary gary gary  fog gary bob bob bob gary gary  the bird in the bush!  lol mate! hrfjgekgjhejgkjvnejgkjeg bob bo b  gary gary gary gary  fog gary *bob bob bob* gary gary  the bird in the bush!  lol mate! 
  • --- * show changed part of text file in output.

whereas want output:

*hee hee hee  ho ho ho* hrfjgekgjhejgkjvnejgkjeg bob bo b  gary gary gary gary  fog gary bob bob bob gary gary  the bird in the bush!  lol mate! 

as can see, each sed command applied independently in each iteration of printing text file. need sed commands applied together.

note how desired header first sed command applied in first iteration, while in second not (but bob bob change is).

why has happened, , can make commands in script cumulative 1 text file? didn't seem happen same-format awk script.

simply: invoking sed twice.

if run commands in terminal 1 after other, end 2 sets of output. script doing same thing in own shell.

you can use {} group commands , put each 1 on new line or use ; commands act on file cumulatively, calling sed once:

#!/bin/bash sed '{      1i hee hee hee\n ho ho ho\n      s/bob/bob/g }' "$1" 

output of ./script file

hee hee hee  ho ho ho  hrfjgekgjhejgkjvnejgkjeg bob bo b  gary gary gary gary  fog gary bob bob bob gary gary  the bird in the bush!  lol mate! 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User