awk '$0 ~ str{print b}{b=$0}' str="findme" path_to_file
with this I can get the line before the found string's line.
How can I print its line number?
Join Stack Overflow to learn, share knowledge, and build your career.
|
Use
|
|||||||||
|
|
If I interpret the question correctly, and you simply want to print the line number of the line that precedes any line containing a given string, you can do:
$ awk '/findme/{print NR - 1}' /path/to/file
|
|||||
|
|
Here is a solution:
Or, if you don't mind a slightly different output, in which there are '--' separating groups of found lines:
In this case, you search for the string "findme" within the file 'path_to_file'. The -B1 flag says, "also prints 1 line before that." |
|||
|
|
|
you can use |
|||||
|
|
Just use |
|||
|
|
print, e.g.awk -v str="findme" '$0 ~ str {print}' path_to_file. But if you want the line number (question is wrong then), useNRbuilt-in variable. – przemoc May 28 '11 at 11:49with this i can get the line before the found string's line. How can i also print its line?It was fixed by @Johan 6.5h after my comment was sent. After commenting under @eyadof answer you should realize that your question needs fixing, and my comment back then should convince you even more to better phrase it. Be always precise as much as you can and don't pretend you were when actually you weren't. It's easy to check (especially by yourself as the OP), so I don't know why are you bringing this up now. – przemoc Jun 3 '11 at 17:13