Tuesday, October 23, 2018

#1 2018-08-12 10:08:38 am

SunnyFrinton
Member
Registered: 2012-09-13
Posts: 26

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

Offline

 

#2 2018-08-12 11:55:56 am

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

Re: Problem with escape characters in do shell script

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 …

Applescript:

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

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

Applescript:

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:

Applescript:

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

… rather than …

Applescript:

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

Last edited by Nigel Garvey (2018-08-12 11:57:40 am)


NG

Offline

 

#3 2018-08-13 09:09:18 am

SunnyFrinton
Member
Registered: 2012-09-13
Posts: 26

Re: Problem with escape characters in do shell script

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

Offline

 

#4 2018-08-13 10:01:08 am

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

Re: Problem with escape characters in do shell script

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?

Applescript:

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


NG

Offline

 

#5 2018-08-13 10:27:22 am

SunnyFrinton
Member
Registered: 2012-09-13
Posts: 26

Re: Problem with escape characters in do shell script

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)