0

On the Unix command line, I want to search for a string "Sample_string" at the beginning of a line in a file, and add a ";" at the end of the line in the file.

This would need to be an in-place edit, affecting a few dozen files in a particular directory.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mohammed
  • 11
  • 3
  • 1
    Welcome to Stack Overflow, it is always great to show us some sample of Inputs and output in CODE TAGS even it is a simple query too. Also try to search solutions on site too, you may get plenty of help, cheers :) – RavinderSingh13 Feb 15 '18 at 07:44
  • see also https://www.gnu.org/software/sed/manual/sed.html#sed-addresses and https://stackoverflow.com/tags/sed/info – Sundeep Feb 15 '18 at 08:27
  • 1
    Possible duplicate of [Add to the end of a line containing a pattern - with sed or awk](https://stackoverflow.com/questions/9591744/add-to-the-end-of-a-line-containing-a-pattern-with-sed-or-awk) – Sundeep Feb 15 '18 at 08:28
  • found the above link as first online search result for `sed search beginning add string at end` – Sundeep Feb 15 '18 at 08:29

1 Answers1

1

Following sed may help you on same:

sed '/^Sample_string/s/$/;/' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • it was almost there. I had to add a -i at the beginning, so it actually needed to be written out as: sed -i '/^Sample_string/s/$/;/' Input_file – Mohammed Feb 15 '18 at 11:01
  • 1
    Thanks bhai, you actually did a great job, and yes you're right, I could've been a little clearer. :) – Mohammed Feb 15 '18 at 11:51