-2

my file Like this:

cheney
fang
130034
target 'cheney11' do
    pod xxx
end
target 'cheney22' do
    pod xxx 
end

and I want to insert a line which is

:git=>www.google.com, :branch=>'master'" afert "target 'cheney11' do

sot that the result looks like this:

cheney
fang
130034
target 'cheney11' do
    :git=>www.google.com, :branch=>'master'
    inerset a line like 
    pod xxx
end
target 'cheney22' do
    pod xxx 
end

note: there are five space before :git=>www.google.com, :branch=>'master'

Richard-Degenne
  • 2,892
  • 2
  • 26
  • 43
cheney
  • 3
  • 2
  • it is **4** spaces before the `":git...."` in your expected output. – Kent Jul 10 '18 at 16:29
  • 1
    Possible duplicate of [Using sed, Insert a line below (or above) the pattern?](https://stackoverflow.com/questions/11694980/using-sed-insert-a-line-below-or-above-the-pattern) – tripleee Jul 10 '18 at 16:49

2 Answers2

0

Could you please try following and let us know if this helps you.

awk '
/target \047cheney11\047 do/{
  print "    :git=>www.google.com, :branch=>\047master\047" ORS $0;
  next
}
1' Input_file

Append > temp_file && mv temp_file Input_file to above code in case you want to save output of it to same Input_file too.

Solution 2nd: Using sed as follows.

sed "s/target 'cheney11' do/    :git=>www.google.com, :branch=>'master'\n&/" Input_file

Kindly use either sed -i.bak to take backup + save changes into Input_file OR sed -i option to only save output into Input_file itself.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

a \
text Append text, which has each embedded newline preceded by a backslash

$ sed "/target 'cheney11'/a \
\    :git=>www.google.com, :branch=>'master'" file

test:

kent$  cat f
cheney
fang
130034
target 'cheney11' do
        pod xxx
end
target 'cheney22' do
        pod xxx
end

kent$  sed "/target 'cheney11'/a \
\    :git=>www.google.com, :branch=>'master'" f
cheney
fang
130034
target 'cheney11' do
    :git=>www.google.com, :branch=>'master'
        pod xxx
end
target 'cheney22' do
        pod xxx
end
Kent
  • 189,393
  • 32
  • 233
  • 301