4

I recently received an error telling me that there was a problem on line 52. I am trying to learn what the best method is for finding lines instead of actually counting them.

In Windows, I often used Notepad++ which displayed the lines directly to the left, so users could easily scroll to the specific line they were looking for -- However I do not see this option in Linux.

Maybe a certain application is needed?


UPDATE: I currently can not open software-store and the only text editor I have is the default nano and gedit. So please restrict your answers to solutions that don't involve me installing software.

**NOTE: ** I am not looking to find a 'String' of some sort. I am looking to quickly find the line NUMBER that the string appears on.

dlundy1
  • 181
  • 2
  • 2
  • 13

6 Answers6

8

Let's create a test file;

$ seq 100 >file

Now, let's display line 52:

$ sed -n 52p file
52

Or:

$ awk 'NR==52' file
52

Or, if you have the file open in vim, you can type 52G to jump to line 52.

Using nano

Suppose that your file is called file.txt. To edit the file in nano with line, column, and character count displayed, run

nano -c file.txt

This is what nano looks like with the cursor on line 7 of a 21-line file: enter image description here

John1024
  • 13,947
7

If you're looking for a GUI approach, you can display line numbers in the default text editor, gedit. To do this, go to Edit -> Preferences and tick the box that says "Display line numbers."

You can also jump to a specific line number by using Ctrl+I.

Chuck R
  • 5,038
5

You can display the file with less. Use less -N to display line numbers, type "52" to less to get to line 52. See man less.

Or, you could open the file with the vim editor (type vim thefile), then typing ":52" will get you to line 52. See man vim

waltinator
  • 37,856
5

In vi or vim

:set nu turns on line numbers

:set nonu turns off line numbers

52gg goes to line 52

52G also goes to line 52

1

Hey everyone I think many of us completely over-thought this. As I continued reading through your comments and Answers, I realized that the basic text editor tells you what line and Column you are on if you look at the bottom of the window. :/

I feel silly lol

dlundy1
  • 181
  • 2
  • 2
  • 13
0

A good tool for the job is grep. With grep -i -n 'string' you can search for a particular string and find out what line it is on.

Suppose java compiler reports me an error that in line System.out.println("Hello World") I have no semicolon or something like that.

What I could do is to cat helloworld.java | grep -i -n 'hello world'

And this would return me 7: System.out.println("Hello World!") , where 7: is the line number with colon as separator.

Alternatively, you can start nano with cursor on a particular line and column like so nano +7,0 helloworld.java (and by the way this is one of the first options in the man nano)