【问题标题】:awk to merge lines based on 1st characterawk 根据第一个字符合并行
【发布时间】:2012-12-21 13:51:17
【问题描述】:

我正在尝试编写一个命令来将文本程序包文件转换为自动密钥文件。 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

【问题讨论】:

  • 我不确定我是否理解你的问题。使用上面的示例输入,您是否希望创建 4 个自动键文件?如果我没有正确理解你,你能用一些预期的输出来更新你的问题吗?干杯。

标签: regex bash ssh awk


【解决方案1】:

这可能对你有用:

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

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

【讨论】:

  • 这非常适合将缩写及其对应的替换文本移动到自己的文件中。
【解决方案2】:

尝试使用"\n\." 作为awk 的记录分隔符正则表达式。

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 2016-07-02
    • 1970-01-01
    相关资源
    最近更新 更多