问题
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