有这个输入:
bar foo
foo ABC/DEF
BAR ABC
ABC foo DEF
foo bar
在上面我需要做4(顺序)动作:
[x]
我能够解决上述问题,但需要两次sed
次调用。脚本:
#!/bin/bash
data() {
cat <<EOF
bar foo
foo ABC/DEF
BAR ABC
ABC foo DEF
foo bar
EOF
}
echo "Result OK"
data | sed -n '/foo/s/[^A-Z]//gp' | sed '/^\s*$/d;s/./[&]/g'
# in the above it is solved using 2 sed invocations
# trying to solve it using only one invocation,
# but the following doesn't do what i need.. :( :(
echo "Variant 2 - trying to use only ONE invocation of sed"
data | sed -n '/foo/s/[^A-Z]//g;/^\s*$/d;s/./[&]/gp'
以上输出:
Result OK
[A][B][C][D][E][F]
[A][B][C][D][E][F]
Variant 2 - trying to use only ONE invocation of sed
[A][B][C][D][E][F]
[B][A][R][ ][A][B][C]
[A][B][C][D][E][F]
变体2也应该只是
[A][B][C][D][E][F]
[A][B][C][D][E][F]
只能通过一次sed
调用来解决上述问题吗?
答案 0 :(得分:2)
替代 sed 方法:
sed '/foo/!d;s/[^A-Z]//g;/./!d;s/./[&]/g' file
输出:
[A][B][C][D][E][F]
[A][B][C][D][E][F]
/foo/!d
- 删除所有不包含foo
/./!d
- 删除所有空行
答案 1 :(得分:1)
sed -n '/foo/{s/[^A-Z]//g;/^$/d;s/./[&]/g;p;}' inputfile
输出:
[A][B][C][D][E][F]
[A][B][C][D][E][F]