Displaying a Line in Linux
Here’s an interesting thing. Let’s say you have a problem like I did, above. An error pops up in a big project, referring to a specific line number of a generic filename. How generic? In Linux, we can see exactly how many of those files with that name exist with this command:
vagrant@homestead:~/$ find . -name Client.php |
OOh. Quite a few! One option is that I open each of them up in my favourite text editor, and scroll down (or jump) to that line: 131. Sounds OK for a small number of them, but I can show you another way which may work out easier.
To start, how do we show a given line in a text file? For that, we can use a utility like sed
.
vagrant@homestead:~/$ sed -n '131,131p' ./vendor/guzzlehttp/guzzle/src/Client.php |
Using this tool, we are asking it to print out from line 131, to line 131, of the file: ./vendor/guzzlehttp/guzzle/src/Client.php
. In effect, we are just printing out one line.
That’s helpful and a little easier than opening up the editor, I guess, but there is a way to combine both commands; in effect, find me all the files named Client.php
AND show that line number. We can do that with this:
vagrant@homestead:~/$ find . -name Client.php -exec sed -n '131,131p' {} \; |
All the magic happens with the -exec
command line option and this part: {} \;
which replaces each filename , at that position of the executing command.
Did that solve my problem? No, but it might yours :-)
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com