Hi guys,
does anyone know how to extract(grep) a line from the file, if I know the line number?
Thanks a lot.
ln=14
sed '${ln}!d' file
Thanks a lot
Possibly :
ln=14
sed -n "${ln}p" file
is more efficient?
Not really. If you were dealing with a very large file and there were many lines remaining after printing the target, both approaches could continue reading the entire file. I say "could" because some sed implementations, when using -n, will abort once they know that there's no possibility of an address matching further input (I believe I saw such an optimization in gnu sed a while back).
To ensure that sed doesn't continue reading needlessly, use q:
sed -n "$ln{p; q;}" file
Regards,
Alister
awk -vln=12 'NF==ln{print; exit}'
This is what I was referring to http://git.savannah.gnu.org/cgit/sed.git/tree/sed/execute.c\#n1774
#ifdef EXPERIMENTAL_DASH_N_OPTIMIZATION
/* If our top-level program consists solely of commands with
ADDR_IS_NUM addresses then once we past the last mentioned
line we should be able to quit if no_default_output is true,
or otherwise quickly copy input to output. Now whether this
optimization is a win or not depends on how cheaply we can
implement this for the cases where it doesn't help, as
compared against how much time is saved. One semantic
difference (which I think is an improvement) is that *this*
version will terminate after printing line two in the script
"yes | sed -n 2p".
Don't use this when in-place editing is active, because line
numbers restart each time then. */
Although it seems that this code has been bug-ridden since its inception and is currently #undef'd, http://git.savannah.gnu.org/cgit/sed.git/tree/sed/execute.c\#n19
#undef EXPERIMENTAL_DASH_N_OPTIMIZATION /*don't use -- is very buggy*/
Tomorrow will be 13 years to the day since it first appeared, only to be undefined a month later. I wonder why they didn't just yank it completely.
Tue Apr 14 17:34:54 PDT 1998 Ken Pizzini <ken@gnu.org>
* execute.c (execute_program, process_files, count_branches,
shrink_program): Added a first attempt at program optimization.
... <snip> ...
The code is all conditionally compiled based on the
EXPERIMENTAL_DASH_N_OPTIMIZATION symbol being #defined,
so it can be easily omitted if it causes problems.
Sat May 23 16:07:09 HST 1998 Ken Pizzini <ken@gnu.org>
* sed/execute.c: #undef'd EXPERIMENTAL_DASH_N_OPTIMIZATION
because its code is buggy.
Sat May 30 12:23:16 PDT 1998 Ken Pizzini <ken@gnu.org>
* sed/execute.c[EXPERIMENTAL_DASH_N_OPTIMIZATION conditional
code]: various modifications intended to keep this
code in sync with the new changes, but the code still
retains its previous bugs.
Well, at least now I know that I did not imagine it. I did see this optimization somewhere. So, long long story short, use 'q' when you know you no longer need to continue reading the input.
Regards,
Alister
I think you meant to use NR
awk -vln=12 'NR==ln{print;exit}'
line=3
ruby -ne 'print; exit if $.=='"$line"' ' file
:DYes, it is NR, I didn't test the code.
Thanks