Apple/shell script to repleace text inside text file

hello everyone i need to find and replace text inside a text file i tried using sed but due to empry space/ sepcial characters it miserably fails .
can anyone suggest apple or java script which work treating text i want to replace as literal or correct my shell syntax.
text i want to replace :
newTabItem,
BrowserMenuDivider(),
bookmarksItem,
historyItem,
replace with:
bookmarksItem,
historyItem,
newTabItem,
BrowserMenuDivider(),

(see scrennshot of original document):https://i.imgur.com/PBtO4Ji.png
my shell code is:

cd ~/fenix/app/src/main/java/org/mozilla/fenix/components/toolbar
grep -ilr --include='DefaultToolbarMenu.kt' 'newTabItem,BrowserMenuDivider(),bookmarksItem,historyItem,' * | xargs -I@ sed -i ' ' 's/newTabItem,BrowserMenuDivider(),bookmarksItem,historyItem,/bookmarksItem,historyItem,newTabItem,BrowserMenuDivider(),/g' @

thanks for the help

If you just want to do a literal replace, you can easily accomplish that within applescript. Set the text item delimiters to your literal string and break your source text around that and reassemble the pieces around the replacement text. You can reset the TID to something normal afterward.

set sourceText to "shell syntax.
text i want to replace :
               newTabItem,
                BrowserMenuDivider(),
                bookmarksItem,
                historyItem,"

set AppleScript's text item delimiters to "newTabItem,
                BrowserMenuDivider(),
                bookmarksItem,
                historyItem,"

set goodText to "bookmarksItem,
                historyItem,
                newTabItem,
                BrowserMenuDivider(),"

text item 1 of sourceText & goodText & text item 2 of sourceText as text

ale82to. If the white-space characters before each search string are known, and if you want to retain those white-space characters, the following should do what you want. For test purposes, the following script assumes that each line of the search string begins with a tab.

use framework "Foundation"
use scripting additions

set oldString to "    newTabItem,
   BrowserMenuDivider(),
   bookmarksItem,
   historyItem,"

set newString to "    bookmarksItem,
   historyItem,
   newTabItem,
   BrowserMenuDivider(),"

set theFile to POSIX path of (choose file)

set fileContents to current application's NSString's stringWithContentsOfFile:theFile encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

fileContents's writeToFile:theFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)

There are a lot of ways this could be done, and the above script could be significantly enhanced, but you need to provide additional information as to your requirements.

Hi.

sed can do it, but it’s a line editor, so if you want to change the order of some lines, you have to gather them (including the linefeed separators) into the pattern space at the same time. Assuming the lines you want to replace reliably occur as a group in that order and are separated by linefeeds:

set someText to "if (should useBottomToolbar) null else menuToolba
	newTabItem,
	BrowserMenuDivider(),
	bookmarksItem,
	historyItem,
	extensionsItem,
	syncMenuItem, …"

do shell script "echo " & quoted form of someText & ¬
	" | sed -n '/newTabItem/ {  N; h; n; N; G; }; p;'"
-->
"if (should useBottomToolbar) null else menuToolba
	bookmarksItem,
	historyItem,
	newTabItem,
	BrowserMenuDivider(),
	extensionsItem,
	syncMenuItem, …"

The sed code breaks down like this:

Hey Nigel,

Good explanation. Thank you.

-Chris