我有一个包含一行的文本文件。
<input type="button" value = "-" onclick="minusbot()" />
<input type="number" name="kgcount" id = "kilohere" value = "1" >
<input type="button" value = "+" onclick="plusbot()" />
<script>
function minusbot()
{
document.getElementById('kilohere').value = parseInt(document.getElementById('kilohere').value) - 1;
var checknumifzero = parseInt(document.getElementById('kilohere').value);
if(checknumifzero < 1) //preventing to get negative value
{
document.getElementById('kilohere').value = 1;
}
}
function plusbot()
{
document.getElementById('kilohere').value = parseInt(document.getElementById('kilohere').value) + 1;
var checknumiffifty = parseInt(document.getElementById('kilohere').value);
if(checknumiffifty > 50) //just a limit
{
document.getElementById('kilohere').value = 50;
}
}
</script>
我尝试过awk,但它仅显示最后一部分:
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 '{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
答案 0 :(得分:1)
此命令在更一般的情况下有效(例如,如果没有Param1
,Param2
但有实际标识符),并且可以更精确地匹配您的预期输出:
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'
该命令有4个单独的表达式,以;
分隔:
s/ \(Login successfully\|Program Terminated...\)//g
-删除不需要的Login successfully
和Program Terminated...
,并在其前面留一个空格s/\[ /[/g
-删除[
后的空格s/\(\[\|[Ss]tate\)/\n\1/g
-在State
或state
s/\([A-Z][A-Za-z0-9]* :\)/\n\1/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\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
答案 1 :(得分:0)
将[<space>
替换为[
,并在字符串[
,Param
和State
之前添加换行符:
$ 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...