0

Please give me suggestion. I need a working solution for sed.exe command line utility (SED build for Windows, version 4.1.5.4013). Or just give me a working UNIX-solution and i'll try to adapt it for my sed (for Win OS).

How to add the new (pretty sophisticated, to be honest) text line after specified line in a text file?

I have input file that contains following text (two lines):

[Pagebar Item Popup Menu]
Feature !Private, Item, MI_IDM_MENU_PAGEBAR_NEW_PAGE    = New Page, 1,,,"New item menu"

I need to insert following line between first and second lines (above mentioned ):

Item, "Load tab" = Enable Javascript,,,,"Remove from Start Page.rtl" & Enable   plugins & Enable display images & Reload

Is it possible to replace(but not to add new line every time when sed's command running!) an existing first line with two lines (replaced line + the new added string). Or, for an example, to make check if "Item, "Load tab"....." line already exists, avoid undesirable duplicates of the new line which is being inserted?

What I've tried but it didn't work:

sed -i "s/"\[Pagebar Item Popup Menu\]"/"\[Pagebar Item Popup Menu\]"\n"^Item,"Load tab"=Enable Javascript,,,,"Remove from Start Page\.rtl" & Enable plugins & Enable display images & Reload[ 0-9]\{1,\}[.,][0-9]\{3\}$/g" standard_menu.ini
Thor
  • 45,082
  • 11
  • 119
  • 130
Primorsky
  • 1
  • 1

2 Answers2

1
sed -e "/pattern/ {p;s/.*/line_to_insert/;}" input_file > output_file

When pattern matches sed will do the following:

  1. p: print pattern space: in this case this is the matching line
  2. s/.*/line_to_insert/: changes pattern space to line_to_insert

If sed was called without -n then pattern space will be written out

If sed was called with -n then an additional p; command will be needed:

sed -e "/pattern/ {p;s/.*/line_to_insert/;p;}" input_file > output_file
amorfo
  • 11
  • 2
0

For a unix solution under windows have you considered cygwin?

https://www.cygwin.com/

Secondly, your sed command looks a bit funky - have you tried making it easier on the eyes by using a different set of quotes to surround the expression?

finally, I think you might want cygwin as I don't believe sed alone will do what you want.

How to insert a line in a file between two blocks of known lines (if not already inserted previously), using bash?

This example uses grep as well

Thanks,

//P

Community
  • 1
  • 1
YFP
  • 331
  • 3
  • 8