Re: Piping sed to sed commands
Re: Piping sed to sed commands
- Subject: Re: Piping sed to sed commands
- From: BJ Terry <email@hidden>
- Date: Sat, 17 Jan 2004 00:00:34 -0800
There are actually two issues with your code, as far as I can tell. The
first issue is that sed never exits without a control-c being sent to
it, because you have the input file in the wrong place, it should be
specified as an argument to the first file. The next issue is that I
don't believe those regular expressions do what you think they do.
There are a few issues with your regular expressions. First, I couldn't
get them to compile without removing the "\\" before the backslash
which is meant to end the sed expression. Inserting a "\\" escapes the
backslash, thus causing a neverending expression. Even after this is
done, the output looks like this:
<key>Track ID</key><integer>456</integer>
<td class=Name>
<td class=Artist>
<key>Album</key><string>Just Won't Burn</string>
<key>Genre</key><string>Rock Blues</string>
<key>Kind</key><string>AAC audio file</string>
I assume this isn't what you want. What you probably want is for the
original name to be retained, bracketed by <td class=Name> and </td>.
In order to accomplish that, you have to rewrite your regular
expressions like this one, shown exploded so it's easier to understand:
sed '
s/ #open replacement
<key>Name<\/key><string> #match opening text
\( #capturing parenthesis
[^<] #any character except "<" (assumes "<" isn't in any names)
*\)
<\/string> #closing text
/<td class=Name>\1<\/td>/' #/1 is the text captured by the parentheses
Otherwise written as:
sed 's/<key>Name<\/key><string>\([^<]*\)<\/string>/<td
class=Name>\1<\/td>/'
If you really just need Artist and Name, you can do it like this:
sed 's/<key>Name<\/key><string>\([^<]*\)<\/string>/<td
class=Name>\1<\/td>/' untitled.txt | sed
's/<key>Artist<\/key><string>\([^<]*\)<\/string/<td
class=Artist>\1<\/td>/'
Replacing "untitled.txt" with your infile, and adding > outfile to the
end, making appropriate escapes for AppleScript. Notice that the
untitled.txt (my test file) was given as an argument to the first call,
not the second.
Presumably, though, you actually want to do this with all the keys, in
which case you probably don't actually want to write a separate regex
for each type of line, so you can write one like this:
sed 's/<key>\([^<]*\)<\/key><[^>]*>\([^<]*\)<\/[^>]*>/<td
class="\1">\2<\/td>/'
The first set of capturing parentheses capture the id, while the second
set capture the text within the next set of brackets. Running this on
your test data produces:
<td class="Track ID">456</td>
<td class="Name">Rock Me Right</td>
<td class="Artist">Susan Tedeschi</td>
<td class="Album">Just Won't Burn</td>
<td class="Genre">Rock Blues</td>
<td class="Kind">AAC audio file</td>
Notice in particular that instead of hard-coding <string> in the regex
here, I changed it to <[^>]*> in order to match for integer or whatever
other unknown values you may have. I put quotes around the classes
too,. I'm not sure if that is necessary, but it seemed like a good idea
since "Track ID" is two words. You can take them out quite easily.
Anyway, putting that back into your applescript:
do shell script "sed
's/<key>\\([^<]*\\)<\\/key><[^>]*>\\([^<]*\\)<\/[^>]*>/<td
class=\"\\1\">\\2<\/td>/' " & inPOSIX & " > " & outPOSIX
I escaped that manually, so I hope I made the right escapes, but it's
hard to say for sure. Good luck.
BJ
On Jan 16, 2004, at 8:45 PM, Gnarlodious wrote:
>
Entity BJ Terry spoke thus:
>
>
> GNU sed doesn't support '\t'.
>
Fine, I'm solving this another way.
>
>
I'm having a problem piping sed output to another sed command. here's
>
what
>
I'm doing:
>
>
do shell script "sed -e
>
's/.*<key>Name<\\/key><string>\\(.*\\)<\\/string>/<td class=Name>\\/'
>
| sed
>
's/.*<key>Artist<\\/key><string>\\(.*\\)<\\/string>/<td
>
class=Artist>\\/' "
>
& inPOSIX & ">" & outPOSIX
>
>
The first command works but the rest are ignored. I see examples of
>
piped
>
sed out-in on web examples which suggests it should work.
>
>
What is wrong here?
>
>
-- Gnarlie
>
http://www.Gnarlodious.com/iTunes.html
>
>
<key>Track ID</key><integer>456</integer>
>
<key>Name</key><string>Rock Me Right</string>
>
<key>Artist</key><string>Susan Tedeschi</string>
>
<key>Album</key><string>Just Won't Burn</string>
>
<key>Genre</key><string>Rock Blues</string>
>
<key>Kind</key><string>AAC audio file</string>
>
_______________________________________________
>
applescript-users mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
>
Do not post admin requests to the list. They will be ignored.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.