Sed - An Introduction and Tutorial by Bruce Barnett
|
The "=" command prints the current line number to standard output. One way to find out the line numbers that contain a pattern is to use: # add line numbers first,# then use grep,# then just print the number
cat -n file | grep 'PATTERN' | awk '{print $1}'
The?sed?solution is: sed -n '/PATTERN/ =' file Earlier I used the following to find the number of lines in a file #!/bin/sh
lines=`wc -l file | awk '{print $1}' `
Using the "=" command can simplify this: #!/bin/sh lines=`sed -n '$=' file ` The "=" command only accepts one address,so if you want to print the number for a range of lines,you must use the curly braces: #!/bin/sh
# Just print the line numbers
sed -n '/begin/,/end/ {
=
d
}' file
Since the "=" command only prints to standard output,you cannot print the line number on the same line as the pattern. You need to edit multi-line patterns to do this. (编辑:网站开发网_马鞍山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

