重新格式化这个字符串
Reformat ini string
我有一个包含一行的文本文件。
2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated...
我试过 awk 但它只打印最后一部分:
awk '{for(i=1;i<=NF;i++) {if ($i == "Section") beginning=i; if($i== "OPEN") ending=i }; for (j=beginning;j<=ending;j++) printf $j" ";printf "\n" }'
Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN
如何重新格式化为这种模式?
2019-06-19 09:00
[Session 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State : OPEN
...
...
[Session 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State : OPEN
将[<space>
替换为[
并在[
、Param
和State
字符串前添加换行符:
$ sed 's/\[ /[/g; s/\(\[\|Param\|State\)/\n/g' <<<'2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated...'
2019-06-19 09:00 Login successfully
[Section 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 2]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 3]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 4]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 5]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN Program Terminated...
此命令适用于更一般的情况(例如,如果没有 Param1
、Param2
但实际标识符)并更精确地匹配您的预期输出:
sed 's/ \(Login successfully\|Program Terminated...\)//g; s/\[ /[/g; s/\(\[\|[Ss]tate\)/\n/g; s/\([A-Z][A-Za-z0-9]* :\)/\n/g'
工作原理:
该命令有 4 个单独的表达式,由 ;
:
分隔
s/ \(Login successfully\|Program Terminated...\)//g
- 删除不需要的 Login successfully
和 Program Terminated...
,前面有 space
s/\[ /[/g
- 在 [
之后删除 space
s/\(\[\|[Ss]tate\)/\n/g
- 在 State
或 state
之前添加换行符
s/\([A-Z][A-Za-z0-9]* :\)/\n/g
- 在 "Params" 之前添加一个换行符,其名称由大写字母 ([A-Z]
) 和任何 ASCII 字母或数字 ([A-Za-z0-9]
)
示例:
$ STRING="2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated..."
$ echo $STRING | sed 's/ \(Login successfully\|Program Terminated...\)//g; s/\[ /[/g; s/\(\[\|[Ss]tate\)/\n/g; s/\([A-Z][A-Za-z0-9]* :\)/\n/g'
2019-06-19 09:00
[Section 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 2]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 3]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 4]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 5]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
我有一个包含一行的文本文件。
2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated...
我试过 awk 但它只打印最后一部分:
awk '{for(i=1;i<=NF;i++) {if ($i == "Section") beginning=i; if($i== "OPEN") ending=i }; for (j=beginning;j<=ending;j++) printf $j" ";printf "\n" }'
Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN
如何重新格式化为这种模式?
2019-06-19 09:00
[Session 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State : OPEN
...
...
[Session 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State : OPEN
将[<space>
替换为[
并在[
、Param
和State
字符串前添加换行符:
$ sed 's/\[ /[/g; s/\(\[\|Param\|State\)/\n/g' <<<'2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated...'
2019-06-19 09:00 Login successfully
[Section 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 2]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 3]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 4]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 5]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN Program Terminated...
此命令适用于更一般的情况(例如,如果没有 Param1
、Param2
但实际标识符)并更精确地匹配您的预期输出:
sed 's/ \(Login successfully\|Program Terminated...\)//g; s/\[ /[/g; s/\(\[\|[Ss]tate\)/\n/g; s/\([A-Z][A-Za-z0-9]* :\)/\n/g'
工作原理:
该命令有 4 个单独的表达式,由 ;
:
s/ \(Login successfully\|Program Terminated...\)//g
- 删除不需要的Login successfully
和Program Terminated...
,前面有 spaces/\[ /[/g
- 在[
之后删除 space
s/\(\[\|[Ss]tate\)/\n/g
- 在State
或state
之前添加换行符
s/\([A-Z][A-Za-z0-9]* :\)/\n/g
- 在 "Params" 之前添加一个换行符,其名称由大写字母 ([A-Z]
) 和任何 ASCII 字母或数字 ([A-Za-z0-9]
)
示例:
$ STRING="2019-06-19 09:00 Login successfully [ Section 1] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 2] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 3] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 4] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 5] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN [ Section 6] Param1 : xxxx Param2 : xxxx Param3 : xxxx Param4 : xxxx State: OPEN Program Terminated..."
$ echo $STRING | sed 's/ \(Login successfully\|Program Terminated...\)//g; s/\[ /[/g; s/\(\[\|[Ss]tate\)/\n/g; s/\([A-Z][A-Za-z0-9]* :\)/\n/g'
2019-06-19 09:00
[Section 1]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 2]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 3]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 4]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 5]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN
[Section 6]
Param1 : xxxx
Param2 : xxxx
Param3 : xxxx
Param4 : xxxx
State: OPEN