view a particular line of a file denoted by a number
Solution 1
sed -n 'Xp' theFile
, where X
is your line number and theFile
is your file.
Solution 2
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.
Solution 3
awk one-liner:
awk "NR==$X" file
bash loop:
for ((i=1; i<=X; i++)); do
read l
done < file
echo "$l"
Solution 4
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
Related videos on Youtube
XXL
Updated on September 16, 2020Comments
-
XXL almost 4 years
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?
-
A. Rex over 13 yearsEssentially equivalent to the
sed
one:echo Xp | ed -s file
-
-
Admin over 12 yearsyou can go directly to the line - as the other answer suggests
-
matja over 12 yearswhy was this downvoted? this is the most portable, simplest and most efficient way. one doesn't even need the quotes.
-
fig over 10 years@XXL - so? The point is that it's the best answer, so it should be voted as such.
-
XXL over 10 years@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?!
-
Dan Passaro over 9 years@XXL to reinforce that this is a simple and efficient way to do the task at hand
-
Daniel Fortunov over 6 yearsI 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