如何使用 sed 创建项目符号列表?

如何使用 sed 创建项目符号列表?

我有以下清单:

➜ cat list.txt
job titles
    site reliability engineer
    automation
        automation engineer
        automation architect
        integration specialists

我想得到以下内容list2.txt

* job titles
    * site reliability engineer
    * automation
        * automation engineer
        * automation architect
        * integration specialists

我很确定这对 来说是一份好工作sed。我知道如何获得“线路的开始”

➜ sed 's/^/foo/g' list.txt
foojob titles
foo    site reliability engineer
foo    automation
foo        automation engineer
foo        automation architect
foo        integration specialists

我想我也许能够得到“行的开头,后跟任意数量的字符,直到单词的开头”,但这不起作用。

➜ sed 's/^.*\</foo/g' list.txt
job titles
    site reliability engineer
    automation
        automation engineer
        automation architect
        integration specialists

所以现在我想知道,如何替换“行的开头直到单词的开头,除了附加 * 之外的所有内容”?感谢您的帮助 :)

答案1

一种方法是找到第一个非空白字符,并将项目符号放在它前面

sed 's/[^[:blank:]]/* &/' list.txt
* job titles
    * site reliability engineer
    * automation
        * automation engineer
        * automation architect
        * integration specialists

相关内容