我现在已经使用了sed和awk,但我遇到了以下问题的挑战。我要求有经验的sed / awk大师帮忙。
我有一个文件,其中一些行有数字,有些行没有,如:
afjjdjfj.uihuihi
trfg.rtyhd
0rtgfd.tjbghhh
hbvfd4.rtgbvdgf
00fhfg.fdrgf
rtygfd.ijhniuh
等等
我想从这一个中准确地有两个文件,其中每一行都在两个文件中的一个中表示(没有删除)。
一个包含任何数字0-9的所有行,所以给出上面的文件结果将是:
0rtgfd.tjbghhh
hbvfd4.rtgbvdgf
00fhfg.fdrgf
和另一个包含其余行没有任何数字0-9的文件,所以给定上述文件,它将是:
afjjdjfj.uihuihi
trfg.rtyhd
rtygfd.ijhniuh
我在sed和awk中尝试了不同的策略,没有什么能给我正确的东西。
什么是最好的sed或awk一个班轮来解决这个问题?
感谢您的时间,
汤姆
轻松使用Awk:
awk '/[0-9]/{print > file1; next} {print > file2}' inputfile
使用单个GNU sed命令:
sed -ne '/[0-9]/w with_digits.txt' -e '//!w no_digits.txt' input
结果:
> cat no_digits.txt
afjjdjfj.uihuihi
trfg.rtyhd
rtygfd.ijhniuh
> cat with_digits.txt
0rtgfd.tjbghhh
hbvfd4.rtgbvdgf
00fhfg.fdrgf
w文件名 将模式空间写入文件名。
如果你不介意在输入上运行两次,你可以使用grep:
grep '[0-9]' input > with_digits
grep -v '[0-9]' input > without_digits
perl -MFile::Slurp -lpe '/\d/ ? append_file("digits.txt",$_) : append_file("no_digits.txt",$_)' input.txt