<CR> introduced in grep

Hi Folks

I’m trying to use grep to pull out the non-digit part of a folder name: “140202_InKindRoll” (ie “_InKindRoll” is the part I need).

I’ve used:


set baseFolderName to do shell script "echo " & oldFolderName & " |/usr/bin/grep -o  '[^0-9]'"

and i get carriage returns introduced into the result:

		
"_
I
n
K
i
n
d
R
o
l
l"

any clues as to how to avoid this, or strip it out post-hoc?

thanks!

Ralph

Fortunately Unix also knows the «tr» command :wink:


set oldFolderName to "140202_InKindRoll"
set baseFolderName to (do shell script "echo " & oldFolderName & " | /usr/bin/tr -d [0-9]")

Wow- that was a quick response. Thanks Martin!

it works!!

-ralph

ok, maybe I crowed a bit too soon:

when I have a file name such as

140202_InKindRoll33

it strips out the ending “33”

I tried using the ‘beginning of line’ carat:

set baseFolderName to (do shell script "echo " & oldFolderName & " | /usr/bin/tr -d ^[0-9]")

any clue as to how to restrict it to the first string it finds?

thanks!

Ralph

Hi,

if the underscore character is always the separator, why not a pure AppleScript solution?


set oldFolderName to "140202_InKindRoll33"
set baseFolderName to text (offset of "_" in oldFolderName) thru -1 of oldFolderName

sometimes it’s a " " (space) character. anything but a digit.

after searching google a bit, what’s about this


set oldFolderName to "140202_InKindRoll33"
set baseFolderName to (do shell script "echo " & oldFolderName & " | sed 's/^[0-9]*//'")

Grep returns one line per match; There are multiples matches because your pattern only matches a single character.

Try something like this:

set oldFolderName to "140202_InKindRoll123"

do shell script "echo " & quoted form of oldFolderName & " | /usr/bin/grep --only-matching '[^0-9]\\+$'"
set baseFolderName to result

That said, I’m not sure how reliable that pattern is. StefanK’s previous post should be a good alternative.

set oldFolderName to "140202_InKindRoll123"

do shell script "echo " & quoted form of oldFolderName & " | /usr/bin/sed 's/^[0-9]\\{1,\\}//'"
set baseFolderName to result

PS: in a repeat loop with a hugh amount of iterations this could be faster.


set oldFolderName to "140202_InKindRoll123"
set baseFolderName to skipLeadingDigits(oldFolderName)

on skipLeadingDigits(d)
	repeat with i from 1 to count characters of d
		try
			character i of d as integer
		on error
			return text i thru -1 of d
		end try
	end repeat
end skipLeadingDigits

wow. that really works. Ok Stefan- can you explain the regex magic: sed ‘s/^[0-9]*//’ please?

thanks!

Ralph

from the sed man page:

[2addr]s/regular expression/replacement/flags
Substitute the replacement string for the first instance of the
regular expression in the pattern space. Any character other
than backslash or newline can be used instead of a slash to
delimit the RE and the replacement. Within the RE and the
replacement, the RE delimiter itself can be used as a literal
character if it is preceded by a backslash.

regular expression; ^[0-9]* that means all(*) digits([0-9]) at the beginning(^)
replacement: none
flags: none

got it. the “//” = “replace with nothing”. very cool.

thanks!

-Ralph