Hi,
I try to find a certain line of text - beginning with a specific text - in a text file and output it to a applescript variable via »do shell script« (because it’s incredibly fast!). Now I tried several find shell commands (awk, sed) to do this job. Everything works fine, but when a »true« mac file comes along, I founder on the line terminator problem: awk and sed need UNIX terminations »\n«, but Mac file use »\r«. I tried to use awk with my searchterm after a pipe:
set myResult to do shell script "sed 'y/\\r/\\n/' '" & myFile & "' | awk '/^SEARCHTERM/'"
But whatever I do (»sed ‘g/\r/\n’ …« or »awk ‘{sub("\r","\n"}’ …) I can’t get this to work.
Has someone any idea or a tip, how I could handle this?
Thanks!
Tobias
AppleScript: 1.10.7
Browser: Safari 523.12.2
Operating System: Mac OS X (10.4)
Hi Tobias,
try something like this
set a to "Apple" & return & "Cherry" & return & "Grapefruit"
set b to do shell script "/bin/echo " & quoted form of a & " | /usr/bin/tr '\\r' '\\n' |/usr/bin/awk '/fruit/'"
--> Grapefruit
Thanks Stefan, tr works:
set myResult to do shell script "cat '" & myFile & "' | /usr/bin/tr '\\r' '\\n' | awk '/^SEARCHTERM/'"
Great!
Tobias
sed has problems with \n and \t so I use perl instead:
sed -e ‘s/\n//g’
perl -pe ‘s/\n//g’
Hi Tobias,
if you like to do the job with AWK, no need to use it’s ‘sub’ or ‘gsub’ build-in function, just start your statement by declaring the ‘Record Separator’ (RS) variable in the ‘BEGIN’ pattern.
set a to "Apple" & return & "Cherry" & return & "Grapefruit" & return & "Lemon" & return & "Passion fruit"
set b to do shell script "/bin/echo -n " & quoted form of a & " | /usr/bin/awk 'BEGIN {RS=\"\\r\"}; /fruit/'"
-- set b to do shell script "/bin/echo -n " & quoted form of a & " | /usr/bin/awk 'BEGIN {RS=\"\\x0D\"}; /fruit/'" -- hex equivalence of CR
result:
Grapefruit
Passion fruit
hth