Zugehörige Zeilen zusammenführen

Das Problem wurde hier mal beschrieben. Man halt also 2 Tabellen und möchte die Werte aus beiden zusammenführen (addieren in diesem Fall). Nix andere tut dieses Script mit tab-separierten Dateien.
Code zum markieren einmal anklicken Code im Skript-Editor öffnen

set file1 to quoted form of (POSIX path of ((choose file) as alias))

set file2 to quoted form of (POSIX path of ((choose file) as alias))

set mytempfile to "/tmp/a21239d511fe54d3b3ba661bc0a45a31.txt"

set thetext to (do shell script "cat " & file1 & " > " & mytempfile & ";echo ''>>" & mytempfile & ";cat " & file2 & " >>" & mytempfile & "; sort " & mytempfile)


set valueList to buildlist(thetext, tab)


repeat with i from 1 to count of every item of valueList

if i = 1 then

copy {item 1 of valueList} to finalvaluelist

else

if item 1 of item i of valueList = item 1 of last item of finalvaluelist then

set item 2 of last item of finalvaluelist to (item 2 of last item of finalvaluelist) + (item 2 of item i of valueList)

set item 3 of last item of finalvaluelist to (item 3 of last item of finalvaluelist) + (item 3 of item i of valueList)

else

copy finalvaluelist & {item i of valueList} to finalvaluelist

end if

end if

end repeat

set the clipboard to buildtext(finalvaluelist, tab)

display dialog "result is in clipboard"


on buildtext(llist, thedelimiter)

set AppleScript's text item delimiters to thedelimiter

repeat with i from 1 to count of llist

set item i of llist to item i of llist as text

end repeat

set AppleScript's text item delimiters to return

set llist to llist as text

set AppleScript's text item delimiters to ""

return llist

end buildtext


on buildlist(thetext, thedelimiter)

set thelines to every paragraph of thetext

set thevalues to {}

set AppleScript's text item delimiters to thedelimiter

repeat with theline in thelines

set tmp to every text item of theline

if (count of tmp) = 3 then

set thevalues to thevalues & {{item 1 of tmp, (item 2 of tmp) as integer, item 3 of tmp as integer}}

end if

end repeat

set AppleScript's text item delimiters to ""

return thevalues

end buildlist #

hier noch die Beispiel-Dateien zu ausprobieren...Testfile1 Testfile2 Theoretisch könnte man so etwas auch für Excel schreiben und die Daten einfach über die Zwischenablage einlesen....

Kleiner Nachtrag

Code zum markieren einmal anklicken Code im Skript-Editor öffnen

set thetext to (do shell script "tr '\r' '\n' <" & file1 & " > " & mytempfile & ";echo ''>>" & mytempfile & ";tr '\r' '\n' <" & file2 & " >>" & mytempfile & ";sort " & mytempfile)

damit werden carriage returns von mac in normale unix LF umgewandelt ist etwas zuverlässiger, man weiß ja nie, was einem für eine Text-Datei vorgesetzt wird =)
This entry was posted in Useful Snippets and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA:


× six = 36

Subscribe without commenting

  • Seite übersetzen:


    Paypal for Pizza:




  • Kategorien


  • Letzte Kommentare

    • Niklas: Vielen Vielen Dank! So klappt es!
    • hubi: Servus Niklas, ich habe mir den Quellcode noch einmal angesehen und habe nun unter 10.7.3 einen Weg gefunden...
    • Niklas: Klingt super das Script. Leider bekomm ich immer folgende Fehlermeldung: error “„Mail“ hat einen Fehler...
    • Jürgen: Hallo Hubi, beim Abfragen von Kennworten gibt es noch eine böse Falle: Das Format, in dem security antwortet,...
    • hubi: Am einfachsten Du öffnest im AppleScript-Editor mal das Funktionsverzeichnis (unter Ablage) von iTunes. Ein...