假设我有一个像这样的文件:
1 10 20 30 40 50
==================================================
foofoofoo1111111111bblah moreblahblblahblah
foofoofoo2 foofoo stuffhere
================================================== ==
我想返回位置 11-20 和 31-40 为空白的所有行。我可以使用剪切来识别它们:
cut -b 11-20,31-40 < source.txt
返回这些位置的字符。
====================
111111111bmoreblahbl
====================
第二行(忽略 === 的行)全是空白。 我想将这些字符为空白/空格的整行(所以这里的第二行)重定向到一个新文件。我不知道如何结合 cut 和 grep 来做到这一点。这当然是可能的,但我无法解决。
最佳答案
有这样的事吗?使用 awk:
$ awk 'substr($0,11,10) substr($0,31,10)~/^ *$/' file
foofoofoo2 foofoo stuffhere
解释:
$ awk '
substr($0,11,10) substr($0,31,10)~/^ *$/ # positions 11-20 and 31-40 are all space
' file
使用grep
:
$ grep "^.\{10\} \{10\}.\{10\} \{10\}" file
从开头 (^
) 开始,有 10 个任意字符 (.\{10\}
),然后是 10 个空格 (\{10\}
)并重复。
编辑:
grep
的较短版本:
$ grep "^\(.\{10\} \{10\}\)\{2\}" file
关于awk - 使用 cut 过滤固定宽度的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54913120/