Problem with escape characters in do shell script

I have a large csv file, trying to use sed to extract record numbers with particular dates but have come unstuck with slashes. Keep returning error

error “sed: 1: "/07/08/2018/=": invalid command code 0” number 1

I know it’s to do with escaping slashes but cannot find the correct combination, help please.

set filePath to quoted form of “/Users/Ours/Documents/Cats Protection/CS Notepad/Call Details copy.txt”
set searchItem to quoted form of “07/08/2018”
set foundRec to do shell script “sed -n /” & searchItem & "/= " & filePath – get record number

HI.

When you have text bracketed with slashes at the beginning of a sed line in order to identify lines containing that text, you can either escape slashes in the text with backslashes — which themselves have to be escaped for the AppleScript text containing the code …

set searchItem to quoted form of "/07\\/08\\/2018/"

… or you can wrap the search text’s slashes as regex classes:

set searchItem to quoted form of "/07[/]08[/]2018/"

Its the same number of keystrokes. It just depends what looks more readable to you.

In an ‘s’ command, you can simply use a character other than a slash as the command’s delimiter, then you don’t need to escape slashes in the text. Whatever comes after the ‘s’ (with a couple of exceptions) is taken as the delimiter in that instance:

"sed -n " & quoted form of "s|07/08/2018|Hello|p"

… rather than …

"sed -n " & quoted form of "s/07\\/08\\/2018/Hello/p"

Many thanks for reply, unfortunately I cannot get either solutions to work, what am I missing?

error “sed: 1: "//07\/08\/2018//=": invalid command code 0” number 1

or

error “sed: 1: "//07[/]08[/]2018//=": invalid command code 0” number 1

I think it’s confusion caused by us having different ideas about where the single quotes should go in relation to the sed code. Does this work for you?

set filePath to quoted form of "/Users/Ours/Documents/Cats Protection/CS Notepad/Call Details copy.txt"
set searchItem to "07\\/08\\/2018" -- or "07[/]08[/]2018"
set foundRec to do shell script "sed   -n  '/" & searchItem & "/=' " & filePath -- get record number

Works perfectly, many thanks, somebody once said that escape characters were a black art that needed many cups of black coffee!