views:

78

answers:

3

I want to append something at the end of a certain line(have some given character). For example, the text is:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem
Line3:  How to solve the problem?
Line4:  Thanks to all.

Then I want to add "Please help me" at the end of

Line2:  Thanks to all who look into my problem

And "Line2" is the key word. (that is I have to append something by grep this line by key word).

So the text after the script should be:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem Please help me
Line3:  How to solve the problem?
Line4:  Thanks to all.

I know sed can append something to certain line but, if I use sed '/Line2/a\Please help me', it will insert a new line after the line. That is not what I want. I want it to append to the current line.

Could anybody help me with this?

Thanks a lot!

+3  A: 
sed '/Line2/ s/$/ Please help me/'
John Kugelman
John, many thanks for your quick answer. It really works! :-)
zhaojing
+6  A: 

I'd probably go for John's sed solution but, since you asked about awk as well:

$ echo 'Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem
Line3:  How to solve the problem?
Line4:  Thanks to all.' | awk '/^Line2:/{$0=$0" Please help me"}{print}'

This outputs:

Line1:  I just want to make clear of the problem
Line2:  Thanks to all who look into my problem Please help me
Line3:  How to solve the problem?
Line4:  Thanks to all.

An explanation as to how it works may be helpful. Think of the awk script as follows with conditions on the left and commands on the right:

/^Line2:/ {$0=$0" Please help me"}
          {print}

These two awk clauses are executed for every single line processed.

If the line matches the regular expression ^Line2: (meaning "Line2:" at the start of the line), you change $0 by appending your desired string ($0 is the entire line as read into awk).

If the line matches the empty condition (all lines will match this), print is executed. This outputs the current line $0.

So you can see it's just a simple program which modifies the line where necessary and outputs the line, modified or not.


In addition, you may want to use /^Line2:/ as the key even for a sed solution so you don't pick up Line2 in the middle of the text or Line20 through Line29, Line200 through Line299 and so on:

sed '/^Line2:/s/$/ Please help me/'
paxdiablo
paxdiablo, thanks a lot for your kindly help. Your method of awk also works.I have to review awk to understand your method.Thanks :-)
zhaojing
Thanks for paxdiablo again for your so detailed explanation. Yes,/^Line2/ is better. Good suggestion!
zhaojing
@zhaojing, I've added some more detail as to how it works but I'd probably still go for the (modified) `sed` solution. There's little point trying to kill a fly with a thermonuclear warhead :-)
paxdiablo
Yes, paxdiablo, I agree with you. But your awk method helps me to learn much more. Thanks again for your detailed awk explanation! :-)
zhaojing
I think the way of awk of paxdiablo and detailed explanation give me better insight into the problem.I understand the solving method now.Thanks!
zhaojing
+1  A: 

Shell scripting

while read -r line
do 
  case "$line" in
    *Line2* ) line="$line Please help me";;
  esac
  echo "$line"
done <"file" > temp
mv temp file
ghostdog74
ghostdog, many thanks for your shell script. :-). It is of great help.But my reputation is not enough, so I am sorry I have no right to vote for your answer.I think I have learned a lot through all of the answers.Thanks to all.
zhaojing
Thanks to all of the answers. To understand all the answers makes me learn much more besides the problem itself.Since paxdiablo's answer is very detailed to make me more clear about the question, so I vote his answer as accepted.In fact, all the answers are very helpful.
zhaojing