awk基于第一个字符合并线条

时间:2013-01-07 19:23:41

标签: regex bash ssh awk

我正在尝试编写一个命令,将texter包文件转换为autokey文件。 texter文件的格式如下:

.abbr
This would be the replacement text for the line above in texter.
.day
Have a GREAt day!
.but
But some of the 
information takes up multiple lines
.how
However all the abbreviations 
start with a "." and 
then x lines of text to be

我需要将每个文件放在一个单独的文件中,或者将缩写及其替换文本打印成以下格式的相应字段:

  "items": [
{
    "usageCount": 0, 
    "omitTrigger": false, 
    "prompt": false, 
    "description": "description", 
    "abbreviation": {
        "ignoreCase": false, 
        "wordChars": "[\\w]", 
        "immediate": false, 
        "abbreviation": "ABBREVIATION GOES HERE", 
        "backspace": true, 
        "triggerInside": false
    }, 
    "hotkey": {
        "hotKey": null, 
        "modifiers": []
    }, 
    "phrase": "REPLACEMENT TEXT GOES HERE", 
    "modes": [
        1
    ], 
    "showInTrayMenu": false, 
    "matchCase": false, 
    "filter": null, 
    "type": "phrase", 
    "sendMode": "kb"

我很确定我可以使用awk处理格式化部分但是我怎样才能获取正确的缩写及其相应的替换文本。下面是我想出来将它打印到我可以格式化的单个文件但它给我一个关于关闭f的错误:

awk '/^\./ {f="$title\."++d} f{print > f} /^\./ {close f; f=""}' 

这个脚本不会像我写的那样工作:

#!/bin/sh

while read line
do
   if echo $line | grep "^\."; then
       line_no=`echo $line | awk -F "\." '{ print $2 }'
   elif echo $line | grep "^\."; then
       : #ignore
   else
       echo "$line" >> t2ak.$line_no
   fi
done < Default.texter

2 个答案:

答案 0 :(得分:1)

这可能对您有用:

csplit -n5 -z file '/^\./' '{*}'

这会将每个缩写放在一个单独的文件中,然后使用sed或awk使用生成的文件填充模板。

答案 1 :(得分:0)

尝试将"\n\."用作awk的recordseparator正则表达式。