22

Okay, this is probably an evident thing but it escapes me, as in it could probably be done in a much simpler way that I'm not aware of, so far.. Say there's a "file" and I want to view only what's on line number "X" of that file, what would be the solution?

here's what i can think of:

head -X < file | tail -1  
 sed -n Xp < file

is there anything else (or any other way) from the standard set of unix/gnu/linux text-tools/utils?

Belinda
  • 1,230
  • 2
  • 14
  • 25
XXL
  • 1,224
  • 4
  • 12
  • 25

4 Answers4

38

sed -n 'Xp' theFile, where X is your line number and theFile is your file.

Felix
  • 4,510
  • 2
  • 31
  • 46
Jmoney38
  • 3,096
  • 2
  • 25
  • 26
  • why was this downvoted? this is the most portable, simplest and most efficient way. one doesn't even need the quotes. – matja Oct 22 '11 at 19:46
  • 1
    @XXL - so? The point is that it's the best answer, so it should be voted as such. – fig Dec 19 '13 at 15:27
  • 2
    @fig what are you talking about? how is it the best answer? the poster didn't bother reading the OP's post which **already** had this exact part. What's the use of posting the same thing?! – XXL Dec 19 '13 at 16:17
  • 1
    @XXL to reinforce that this *is* a simple and efficient way to do the task at hand – Dan Passaro Oct 10 '14 at 22:27
  • 2
    I rarely look for the answer in the question, so it's useful to have it as a separate answer, that can be voted up or down relative the other answers. +1 – Daniel Fortunov Oct 13 '17 at 06:49
7

in vi:

vi +X filename

in EMACS:

emacs +X filename

in the shell:

nl -ba -nln filename| grep '^X '

you can use context grep cgrep instead of grep to see some lines above and below the matching line..


EXAMPLES:

print just that one line:

$ nl -ba  -nln  active_record.rb  | grep '^111 '
111       module ConnectionAdapters

with context:

$ nl -ba  -nln  active_record.rb  | grep -C 2 '^111 '
109       end
110     
111       module ConnectionAdapters
112         extend ActiveSupport::Autoload
113     

for context control in grep check man grep :

   Context Line Control
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.

       -C NUM, -NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing a group separator (--) between contiguous groups of matches.  With the -o or --only-matching option, this has no effect and a warning is given.
4

awk one-liner:

awk "NR==$X" file

bash loop:

for ((i=1; i<=X; i++)); do
  read l
done < file
echo "$l"
pepoluan
  • 6,132
  • 4
  • 46
  • 76
3

Just use vi

vi file

When in the file type

:X

where X is the line number you want to see

However, the sed -n Xp file is a good way if you really only want to see the one line

Krrl
  • 43
  • 1
  • 5