Command “Print All Lines Starting With” [closed]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 10:14:30

问题


I am looking for a perl command where lines starting with a string are printed. For example, if I wanted to print all lines starting with "1234", what would that command look like? Sed? Awk? Grep?


回答1:


perl -ne 'print if /^1234/' 

or

sed -ne '/^1234/p'

or

awk '/^1234/'

or

grep '^1234'

or

ruby -ne 'print if /^1234/'

or even

python -c 'import fileinput;print "\n".join([l for l in fileinput.input() if l.startswith("1234")])'



回答2:


perl -ne'print if /^1234/' file

But most people would use grep.

grep ^1234 file

Or on Windows

findstr /r ^1234 file



回答3:


For completion:

sed -ne '/^1234/p' somefile
awk '/^1234/{print}' somefile
grep '^1234' somefile


来源:https://stackoverflow.com/questions/12569570/command-print-all-lines-starting-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!