如何使用shell脚本删除一行特定字符的某些字符

时间:2015-01-13 13:36:07

标签: file shell

我有一个名为output.txt的文件,它有一行" test [1,2,3]:[x + y = 0]" 我想删除所有字符直到' ['。并删除']'同样。我希望使用shell脚本在output.txt中只有x + y = 0。我不能删除一些固定的没有。这个output.txt是动态生成的。请提供一些解决方案。

2 个答案:

答案 0 :(得分:0)

Powershell的:

$teststring=teststring.replace("[", "").replace("]", "") -replace ".* "

答案 1 :(得分:0)

这会根据':'分隔符拆分你的行并取第二个标记然后用它替换空格和'['和']'字符什么都没有:

cat output.txt | cut -d':' -f2 | sed -e "s/ //g; s/\[//g; s/\]//g"

打印:

X + Y = 0