how to find and add a string to a file in linux

Posted by user2951644 on Stack Overflow See other posts from Stack Overflow or by user2951644
Published on 2013-11-10T22:37:47Z Indexed on 2013/11/11 21:54 UTC
Read the original article Hit count: 131

Filed under:
|

How can I check a file for a string if missing the string automatically add it for example Input

Input file test.txt

  1. this is a test text for testing purpose
  2. this is a test for testing purpose
  3. this is a test for testing purpose
  4. this is a test text for testing purpose

I would like to add "text" to all the lines

Desired Output

  1. this is a test text for testing purpose
  2. this is a test text for testing purpose
  3. this is a test text for testing purpose
  4. this is a test text for testing purpose

Is it possible? many thanks in advance

Hi guys thanks for all the help, for my case is not that simple. I wont know which line will be different and in the middle string it will not only have a single string. i will give a clearer case

Input file test.txt

  1. Group: IT_DEPT,VIP Role: Viewer Dept: IT
  2. Group: IT_DEPT,VIP Dept: IT
  3. Group: FINANCE LOAN VIEWER Role: Viewer Dept: FINANCE
  4. Group: FINANCE LOAN VIEWER Dept: FINANCE

Desired output file test2.txt

  1. Group: IT_DEPT,VIP Role: Viewer Dept: IT
  2. Group: IT_DEPT,VIP Role: - Dept: IT
  3. Group: FINANCE LOAN VIEWER Role: Viewer Dept: FINANCE
  4. Group: FINANCE LOAN VIEWER Role: - Dept: FINANCE

So those that are missing "Role:" will be added "Role: - ", hope this clear things out, thanks in advance again

© Stack Overflow or respective owner

Related posts about linux

Related posts about shell