Friday, January 28, 2022

#1 2021-08-31 12:54:42 pm

ale82to
Member
Registered: 2012-09-27
Posts: 39

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:

Applescript:

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

Offline

 

#2 2021-08-31 11:15:22 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 176

Re: Apple/shell script to repleace text inside text file

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.


Applescript:

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

Offline

 

#3 2021-09-01 07:00:24 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1218

Re: Apple/shell script to repleace text inside text file

ale82to wrote:

hello everyone i need to find and replace text inside a text file



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.

Applescript:

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.

Last edited by peavine (2021-09-09 09:34:10 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2021-09-01 10:53:15 am

ale82to
Member
Registered: 2012-09-27
Posts: 39

Re: Apple/shell script to repleace text inside text file

peavine wrote:

thank you very much!! seems to work very well now i m gonna test other search/replace!

ale82to wrote:

hello everyone i need to find and replace text inside a text file



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.

Applescript:

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 current application's NSString's stringWithString:fileContents
set fileContents to fileContents's stringByReplacingOccurrencesOfString:oldString withString:newString

(current application's NSString's stringWithString: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.

Last edited by ale82to (2021-09-01 11:01:05 am)

Offline

 

#5 2021-09-01 02:15:31 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5475

Re: Apple/shell script to repleace text inside text file

ale82to wrote:

i tried using sed but due to empry space/ sepcial characters it miserably fails .


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:

Applescript:

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:

# Use sed with automatic output suppressed (-n).
sed -n '/newTabItem/ { # If a line contains “newTabItem”,
    N; # append the next line to it with a linefeed,
    h; # store the result in the hold space,
    n; # output the pattern space (but -n suppresses this) and replace the contents with the next line,
    N; # append the line after that to it with a linefeed,
    G; # and append the hold space contents to the result with a linefeed.
}
p; # Explicitly output the pattern space contents.'


NG

Offline

 

#6 2021-11-26 08:51:43 pm

ccstone
Member
Registered: 2009-02-07
Posts: 417

Re: Apple/shell script to repleace text inside text file

Hey Nigel,

Good explanation.  Thank you.

-Chris

Last edited by ccstone (2021-11-26 08:52:24 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)