4

This question already has an answer here:

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.

marked as duplicate by xangua, karel, heemayl, Tim, Eric Carvalho Apr 18 '15 at 12:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @EliahKagan This is the same as the answer that dlundy1 gave when he answered his own question. Read my comment again, especially the part about the arrow in the screenshot, and then read dlundy1's answer. My comment was intended to be an improvement on dlundy1's answer to his own question as he described it in the question's title. – karel Apr 18 '15 at 8:21
  • @EliahKagan The title of the question is How to Search for Specific LINES in Linux?. In the question dlundy1 also writes So please restrict your answers to solutions that don't involve me installing software. Gedit is installed by default in Ubuntu, so I am showing dlundy1 how to show line numbers in Gedit. – karel Apr 18 '15 at 8:41
  • @karel But that answer is still a workaround for a problem that doesn't apply here. The OP there knew that to show left-side line numbers one uses the Preferences dialog, and the problem was how to access that in the presence of a bug that made the menu item difficult to access. This question is very different--an answer showing how to access it in the normal way (click Edit, click Preferences, check "Display line numbers" in the View tab) would apply here. Closing this as a duplicate of that would only deceive people into thinking that bug still applies. – Eliah Kagan Apr 18 '15 at 8:52
  • Which gedit version you are using? – Pandya Apr 18 '15 at 11:14
  • 2
    voted to re-open. Although the problem is with sources.list , and although the problem is a dup, the question is how to find a line in a text file. IMO the answer is with sed, although grep, awk, perl (to name a few) can do this as can head and tail – Panther Apr 18 '15 at 13:40
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.

  • Thank You! this partially worked. (ctrl + i) However I do not see Edit button in gedit – dlundy1 Apr 18 '15 at 3:53
  • Well, to be honest that was kind of a guess. I run GNOME Shell in the menu is in a different place. Let me boot up a VM. – Chuck R Apr 18 '15 at 4:14
  • In the default desktop environment, Unity, hover over the very top of the screen where it says "Text Editor" and you get your menu bar. It is, indeed Edit->Preferences. In my desktop environment of choice, GNOME Shell, you have to click up top on the activities bar where it says "Gedit" and then go to Preferences. – Chuck R Apr 18 '15 at 4:32
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

  • I don't understand your answer, John. Also because I am unfamiliar with the syntax I have no base of interpreting your scenario into my specific situation. Could you provide a more literal example. also, I am unable to download vim – dlundy1 Apr 18 '15 at 3:44
  • @dlundy1 What is the name of your file? Suppose it was called file.c. In that case, at the command line, run the command sed -n 52p file.c and you will see line 52 displayed. (The sed command is standard with Linux: no install needed.) – John1024 Apr 18 '15 at 4:47
  • @dlundy1 I see that you use nano. I have also added a nano solution for display line numbers. – John1024 Apr 18 '15 at 4:50
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

  • I am unfamiliar with this command, so if you could use it within context that would be great. Also I don't have vim and cannot open software-store until I fix an error related to this post. – dlundy1 Apr 18 '15 at 3:39
  • 2
    @dlundy1 if you don't have vim, you still have vi, which while more basic, still supports navigating to line numbers. – muru Apr 18 '15 at 3:54
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

  • 2
    vi and vim can also be told to show line numbers and to position the cursor at the beginning of a specific line, at the time they are launched: vi '+set nu' +52 (If you like, please feel free to add this information to your answer.) – Eliah Kagan Apr 18 '15 at 5:55
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

  • 1
    Well, could you please find one of the interesting or most practical answers listed here and accept one ? At least for the sake of protocol , you know – Sergiy Kolodyazhnyy Apr 18 '15 at 6:56
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)

  • I didn't downvote it but OP wants to display a line whose number is known already, I think you missunderstood the question – kos Apr 18 '15 at 11:29
  • OP said in the post " I am looking to quickly find the line NUMBER that the string appears on " which is what I addressed. – Sergiy Kolodyazhnyy Apr 18 '15 at 12:14
  • You are right, sorry, in fact it's badly explained and I would have been fooled as well. But actually if you read at the start of the post: "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." and what comes after it's clear that he rather wants a way to display line N – kos Apr 18 '15 at 12:22
  • I left the comment because I saw the non explained downvote, so I tought it would have clarified to you why it was downvoted – kos Apr 18 '15 at 12:27
  • Thank you, kos, I do appreciate when users point out areas where I can improve. – Sergiy Kolodyazhnyy Apr 18 '15 at 14:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.