使用字符串

时间:2015-06-20 05:13:22

标签: regex bash awk sed markdown

我想将包含代码段的可预测格式的文件转换为Markdown。该文件如下所示:

MY CODE SNIPPETS          2015-05-01

This file contains useful code snippets for every day usage
in the Linux command line.

SED
  sed 's/\(.*\)1/\12/g'                  # Modify anystring1 to anystring2
  sed '/^ *#/d; /^ *$/d'                 # Remove comments and blank lines

SORT
  sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # Sort IPV4 ip addresses

...

sedsort开头的行(小写 - 前面可能有空格)应该用```(Markdown开始/结束代码标记)包装,缩进4个空格在该部分之前和之后有1个空白行。 sedsort的连续行应包含在同一编码部分中。最终的Markdown文件应如下所示:

MY CODE SNIPPETS          2015-05-01

This file contains useful code snippets for every day usage
in the Linux command line.

SED

    ```
    sed 's/\(.*\)1/\12/g'                  # Modify anystring1 to anystring2
    sed '/^ *#/d; /^ *$/d'                 # Remove comments and blank lines
    ```

SORT

    ```
    sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # Sort IPV4 ip addresses
    ```

我最感兴趣的是awk/sed/bash解决方案,但欢迎其他建议。

2 个答案:

答案 0 :(得分:2)

也许是这样的:

MY CODE SNIPPETS          2015-05-01

This file contains useful code snippets for every day usage
in the Linux command line.

SED

    ```
    sed 's/\(.*\)1/\12/g'                  # Modify anystring1 to anystring2
    sed '/^ *#/d; /^ *$/d'                 # Remove comments and blank lines
    ```


SORT

    ```
    sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # Sort IPV4 ip addresses
    ```

输出:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".HomePage">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/mod_minecraft"
        android:scaleType="fitXY">
    </ImageView>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_weight="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

答案 1 :(得分:1)

另一个awk变体:
(逻辑与其他答案非常相似)

awk '
    BEGIN{p=0}
    /^ *(sed|sort)/{
        if(!p)print "```";
        p=1;
        print "    " $0;
        next
        }
    p{
        print "```"
        p=0
     }
     1;
     END{
         if(p)print "```"
        }' my_commands.txt

输出:

MY CODE SNIPPETS          2015-05-01

This file contains useful code snippets for every day usage
in the Linux command line.

SED
```
      sed 's/\(.*\)1/\12/g'                  # Modify anystring1 to anystring2
      sed '/^ *#/d; /^ *$/d'                 # Remove comments and blank lines
```

SORT
```
      sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # Sort IPV4 ip addresses
```