-1

I am using below command to search for pattern (Rel_Tag_St_bit) and then append the following line in file:

sed -i -e '/Rel_Tag_St_bit/a\'$'\n''\ methods.mavenWithGoals("mvn so:s -f abc/pom.xml")' file

Once this line is added, I need to have newline character because I see the next line appended after the newly added line on the same line.

Sample input:

Line1 (pattern match)managedScripts.Rel_Tag_St_bit("${env.templo_directory}/version.txt")
Line 2 (append ) methods.mavenWithGoals("mvn so:s -f abc/pom.xml") methods.mavenWithGoals("deploy -DaltDeploymentRepository=)
Line 3 (appears on second line itself) 

So the third line here [methods.mavenWithGoals("deploy -DaltDeploymentRepository=)] appears on the second appended line.

Sample output:

1)managedScripts.Rel_Tag_St_bit("${env.templo_directory}/version.txt")
2)methods.mavenWithGoals("mvn so:s -f abc/pom.xml")
3)methods.mavenWithGoals("deploy -DaltDeploymentRepository=) 
  • 1
    Show some sample input and your corresponding desired output. Also, since different versions of sed handle newlines differently, specify what version of sed you have (GNU or BSD) or, if you don't know that, then specify which platform you are on (MacOS, Linux, BSD, Win...). – John1024 Jun 19 '19 at 16:06
  • 1
    @John1024 I am using MacOS and sample input have been provided but I am not able to format it correctly. – user312307 Jun 19 '19 at 16:20
  • 1
    @user312307 use code formatting: unix.stackexchange.com/editing-help#code – muru Jun 19 '19 at 16:35
  • To be useful to us, the sample input needs to look like you actual input, no added discussion or parenthetical thoughts or line numbers or anything. Likewise, the corresponding sample output needs to look like the actual output that you want. Otherwise, it leaves us guessing. – John1024 Jun 19 '19 at 18:08
0

Assuming your original file contained:

$ cat file
managedScripts.Rel_Tag_St_bit("${env.templo_directory}/version.txt")
methods.mavenWithGoals("deploy -DaltDeploymentRepository=)

You can modify it with:

$ sed -e '/Rel_Tag_St_bit/a\'$'\nmethods.mavenWithGoals("mvn so:s -f abc/pom.xml")\n' file
managedScripts.Rel_Tag_St_bit("${env.templo_directory}/version.txt")
methods.mavenWithGoals("mvn so:s -f abc/pom.xml")
methods.mavenWithGoals("deploy -DaltDeploymentRepository=)

That is using the shell (bash,ksh,zsh) C-strings $'...' to introduce newlines.

  • Thanks..This works :) – user312307 Jun 19 '19 at 18:25
0
$ sed -i -e '/Rel_Tag_St_bit/G;s/\n/&methods.mavenWithGoals("mvn so:s -f abc/pom.xml")/' file

Here you append a newline G at the end of the desired line /Rel_Tag_St_bit/, then stick the desired text after the newline you just added with that G command. Since the newline is added conditionally, so the s/// command won't fire for the noninteresting lines and sed would let them pass on as is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.