2014-10-11 37 views
1

我正在將我的書籤從kippt.com移動到pinboard.in。移動awk文件中的數據塊

我從Kippt導出了我的書籤,由於某種原因,它們在同一字段中存儲了標籤(前面加了#)和說明。插件板將標記和說明分開。

這是一個Kippt書籤貌似出口後:

​​

這是它應該看起來像導入到插件板之前什麼:

<DT><A HREF="http://www.example.org/" ADD_DATE="1412337977" LIST="Bookmarks" TAGS="tag1,tag2">This is a title</A> 
<DD>This is a description 

所以基本上,我需要更換#tag1 #tag2TAGS="tag1,tag2"並在<A>的第一行上移動它。

我一直在閱讀有關搬到這裏來的數據塊:sed or awk to move one chunk of text betwen first pattern pair into second pair?

我一直沒拿出一個好的食譜至今。任何見解?

編輯:

這裏是什麼樣的輸入文件看起來像(3項總分3500)的實際例子:

<DT><A HREF="http://phabricator.org/" ADD_DATE="1412973315" LIST="Bookmarks">Phabricator</A> 
<DD>#bug #tracking 

<DT><A HREF="http://qz.com/261426/the-hidden-commands-for-diagnosing-and-improving-your-netflix-streaming-quality/" ADD_DATE="1412838293" LIST="Inbox">The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz</A> 

<DT><A HREF="http://www.farmholidays.is/" ADD_DATE="1412337977" LIST="Bookmarks">Icelandic Farm Holidays | Local experts in Iceland vacations</A> 
<DD>#iceland #tour #car #drive #self Self-driving tour of Iceland 
+0

是否必須使用'awk' /'sed'或者例如'python'可能嗎? – 2014-10-11 22:04:15

+0

它可能是Python,我開始學習。它可能是另一種語言(Ruby等),但我想學習一種我已經知道的語言:-)謝謝。 – 2014-10-11 22:17:16

+0

在你的例子中,'

#bug#tracking'應該發生什麼?該行應完全刪除還是應導致'
'? – 2014-10-11 22:26:20

回答

0

這可能不是最漂亮的解決方案,但因爲它似乎做一次性事情就足夠了。

import re 
dt = re.compile('^<DT>') 
dd = re.compile('^<DD>') 


with open('bookmarks.xml', 'r') as f: 
    for line in f: 
     if re.match(dt, line): 
      current_dt = line.strip() 
     elif re.match(dd, line): 
      current_dd = line 
      tags = [w for w in line[4:].split(' ') if w.startswith('#')] 
      current_dt = re.sub('(<A[^>]+)>', '\\1 TAGS="' + ','.join([t[1:] for t in tags]) + '">', current_dt) 
      for t in tags: 
       current_dd = current_dd.replace(t + ' ', '') 
      if current_dd.strip() == '<DD>': 
       current_dd = "" 
     else: 
      print current_dt 
      print current_dd 
      current_dt = "" 
      current_dd = "" 

    print current_dt 
    print current_dd 

如果代碼的某些部分不清楚,請告訴我。您當然可以使用python將行寫入文件而不是打印它們,甚至可以修改原始文件。

編輯:添加if子句,以便空的<DD>行不會顯示在結果中。

+0

謝謝Tim!它沒有開箱即用,因爲我的文件中存在一些角落案例(如標籤前的描述,DD缺失),但在某些sed操作之後,我設法得到一個正確的文件,用Python腳本處理一些小修改。 – 2014-10-12 14:25:54

0

script.awk

BEGIN{FS="#"} 

/^<DT>/{ 
    if(d==1) print "<DT>"s # for printing lines with no tags 
    s=substr($0,5);tags="" # Copying the line after "<DT>". You'll know why 
    d=1 
} 

/^<DD>/{ 
    d=0 
    m=match(s,/>/) # Find the end of the HREF descritor first match of ">" 
    for(i=2;i<=NF;i++){sub(/ $/,"",$i);tags=tags","$i} # Concatenate tags 
    td=match(tags,/ /) # Parse for tag description (marked by a preceding space). 
    if(td==0){ # No description exists 
     tags=substr(tags,2) 
     tagdes="" 
    } 
    else{ # Description exists 
     tagdes=substr(tags,td) 
     tags=substr(tags,2,td-2) 
    } 
    print "<DT>" substr(s,1,m-1) ", TAGS=\"" tags "\"" substr(s,m) 
    print "<DD>" tagdes 
} 

awk -f script.awk kippt > pinboard 

INPUT

<DT><A HREF="http://phabricator.org/" ADD_DATE="1412973315" LIST="Bookmarks">Phabricator</A> 
<DD>#bug #tracking 

<DT><A HREF="http://qz.com/261426/the-hidden-commands-for-diagnosing-and-improving-your-netflix-streaming-quality/" ADD_DATE="1412838293" LIST="Inbox">The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz</A> 

<DT><A HREF="http://www.farmholidays.is/" ADD_DATE="1412337977" LIST="Bookmarks">Icelandic Farm Holidays | Local experts in Iceland vacations</A> 
<DD>#iceland #tour #car #drive #self Self-driving tour of Iceland 

OUTPUT:

<DT><A HREF="http://phabricator.org/" ADD_DATE="1412973315" LIST="Bookmarks", TAGS="bug,tracking">Phabricator</A> 
<DD> 
<DT><A HREF="http://qz.com/261426/the-hidden-commands-for-diagnosing-and-improving-your-netflix-streaming-quality/" ADD_DATE="1412838293" LIST="Inbox">The hidden commands for diagnosing and improving your Netflix streaming quality – Quartz</A> 
<DT><A HREF="http://www.farmholidays.is/" ADD_DATE="1412337977" LIST="Bookmarks", TAGS="iceland,tour,car,drive,self">Icelandic Farm Holidays | Local experts in Iceland vacations</A> 
<DD> Self-driving tour of Iceland