extract a line from a file by line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract a line from a file by line number
# 1  
Old 04-13-2011
extract a line from a file by line number

Hi guys,
does anyone know how to extract(grep) a line from the file, if I know the line number?
Thanks a lot.
# 2  
Old 04-13-2011
Code:
ln=14
sed '${ln}!d' file


Last edited by radoulov; 04-14-2011 at 10:03 AM.. Reason: Code tags, please!
# 3  
Old 04-13-2011
Thanks a lot
# 4  
Old 04-13-2011
Possibly :
Code:
ln=14
sed -n "${ln}p" file

is more efficient?
# 5  
Old 04-13-2011
Quote:
Originally Posted by whbos
Possibly :
Code:
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:
Code:
sed -n "$ln{p; q;}" file

Regards,
Alister
# 6  
Old 04-13-2011
Code:
awk -vln=12 'NF==ln{print; exit}'

# 7  
Old 04-13-2011
This is what I was referring to http://git.savannah.gnu.org/cgit/sed...xecute.c#n1774
Code:

#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.../execute.c#n19

Code:
#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.

Code:
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. Smilie

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

3. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

4. Shell Programming and Scripting

Extract a number from a line in a file and sed in another copied file

Dear all, I am trying to extract a number from a line in one file (task 1), duplicate another file (task 2) and replace all instances of the strings 300, in duplicated with the extracted number (task 3). Here is what I have tried so far: for ((k=1;k<4;k++)); do temp=`sed -n "${k}p"... (2 Replies)
Discussion started by: mnaqvi
2 Replies

5. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

8. Shell Programming and Scripting

extract a line from a file using the line number

Hello, I am having trouble extracting a specific line from a file when the line number is known. My first attempt involved grep -n 'hi' (the word 'hi will always be there) to get the line number before the line that I actually want (line 4). Extra Notes: -I am working in a bash script. -The... (7 Replies)
Discussion started by: grandtheftander
7 Replies

9. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

10. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies
Login or Register to Ask a Question