Reputation: 23
I have a text file which contain one line.
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...
I have tried awk but it prints only the last section:
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
How do I reformat to this pattern?
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
Upvotes: 2
Views: 44
Reputation: 2419
This command works in a more general case (for example, if don't have Param1
, Param2
but actual identifiers) and matches your expected output more precisely:
sed 's/ \(Login successfully\|Program Terminated...\)//g; s/\[ /[/g; s/\(\[\|[Ss]tate\)/\n\1/g; s/\([A-Z][A-Za-z0-9]* :\)/\n\1/g'
The command has 4 separate expressions separated by ;
:
s/ \(Login successfully\|Program Terminated...\)//g
- removes unneeded Login successfully
and Program Terminated...
with a space before thems/\[ /[/g
- removes space after [
s/\(\[\|[Ss]tate\)/\n\1/g
- adds a newline before State
or state
s/\([A-Z][A-Za-z0-9]* :\)/\n\1/g
- adds a newline before "Params", where its name consists of uppercase letter ([A-Z]
) and then any ASCII letter or digit ([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\1/g; s/\([A-Z][A-Za-z0-9]* :\)/\n\1/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
Upvotes: 1
Reputation: 141533
Replace [<space>
with [
and add a newline before [
, Param
and State
strings:
$ sed 's/\[ /[/g; s/\(\[\|Param\|State\)/\n\1/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...
Upvotes: 0