I need to insert text at specific position but the line can be anywhere because file is huge 1000 to 2000 lines and information is added in middle or anywhere.

A short version of example below:

 1 define service{
 2         service_description             version-check-aix
 3         use                             passive-service
 4         max_check_attempts              1
 5         initial_state                   o
 6         notifications_enabled           0
 7         host_name                       hosts1,host2
 8 }
10 define service{
11         service_description             version-check-unix
12         use                             passive-service
13         max_check_attempts              1
14         notifications_enabled           0
15         host_name                       hosts1,host2
16 }
17 define service{
18         service_description             version-check-linux
19         use                             passive-service
20         initial_state                   o
21         notifications_enabled           0
22         host_name                       hosts1,host2 

I need to append host3 at end of line which contains host_name<tab><something here>,host3. In my example that would be on line 15. (The line numbers are for illustration and are not present in the data file.)

Can i do grep -in version-check-unix and insert "host3"? How?

closed as unclear what you're asking by Jeff Schaller, Kusalananda, don_crissti, Anthon, DarkHeart May 15 '17 at 0:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please detail what needs to be inserted (always the same, or does it differ) depending on what condition (only on line 15, or whenever ... happens). – Ned64 May 14 '17 at 13:54
  • 1
    grep can only extract, not insert. – Kusalananda May 14 '17 at 13:56
  • 2
    you say "line number 15" but "it can be anywhere" -- so specify as carefully as you can what you want to have happen – Jeff Schaller May 14 '17 at 13:59
  • 1
    You say that you wish to to append "host3" at end of line which contains "host_name,host3" but line 15 does not have this - please correct the specifications. – Ned64 May 14 '17 at 15:33
  • 1
    Possible duplicate of Appending word at the end of line with sed – don_crissti May 14 '17 at 15:37

You wish to append something to a line which matches certain criteria. This can be done with sed, among others.

Let us assume that the match criteria are: The line has the string "host_name" and then (after some spaces and/or TABs) "hosts1,host2"; you wish to add ",host3". Then this would work:

cat file | sed 's/\(^[\t ]\+host_name[\t ]\+hosts1,host2$\)/\1,host3/g' > newfile

where file is the original file and newfile the new, edited file.

  • Please note that you wrote "hosts1" but "host2", if this is a typo please correct this in the command line. – Ned64 May 14 '17 at 16:03
  • thank you i will use this and let you know the result. Yes that was typo mistake – user138530 May 14 '17 at 17:07

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