如何使用 sed 替换文件中以 (*) 开头的列表

Abd*_*oud 4 sed text-processing

使用sed如何替换以星号开头的行并将其替换为从 1 开始的数字?

例如,我需要使用 sed 用数字替换列表中的 (*) >

这个文件包含一个列表

*linux

*computers

*labs

*questions
Run Code Online (Sandbox Code Playgroud)

到 >>>>

这个文件包含一个列表

1 linux
2 computers
3 labs
4 questions
Run Code Online (Sandbox Code Playgroud)

我尝试使用

sed -e 's/*//' file.in > file.out | sed -i = file.out
Run Code Online (Sandbox Code Playgroud)

gle*_*man 8

您可以使用 awk:

awk '/^\*/ {sub(/\*/, ++i)} 1' <<END
A list
* one
* two
Blah
* three
END
Run Code Online (Sandbox Code Playgroud)
A list
1 one
2 two
Blah
3 three
Run Code Online (Sandbox Code Playgroud)


Cos*_*tas 7

你可以玩一些技巧 - 第一个数字线,*然后条带*spaces如果你愿意

nl -bp^[*] file | sed 's/^\s*\|\s*\*//g'
Run Code Online (Sandbox Code Playgroud)