English 中文(简体)
How do I extract lines from a file using their line number on unix?
原标题:

Using sed or similar how would you extract lines from a file? If I wanted lines 1, 5, 1010, 20503 from a file, how would I get these 4 lines?

What if I have a fairly large number of lines I need to extract? If I had a file with 100 lines, each representing a line number that I wanted to extract from another file, how would I do that?

最佳回答

Something like "sed -n 1p;5p;1010p;20503p . Execute the command "man sed" for details.

For your second question, I d transform the input file into a bunch of sed(1) commands to print the lines I wanted.

问题回答

with awk it s as simple as:

awk  NR==1 || NR==5 || NR==1010  "file"

@OP, you can do this easier and more efficiently with awk. so for your first question

awk  NR~/^(1|2|5|1010)$/{print}  file

for 2nd question

awk  FNR==NR{a[$1];next}(FNR in a){print}  file_with_linenr file

This ain t pretty and it could exceed command length limits under some circumstances*:

sed -n "$(while read a; do echo "${a}p;"; done < line_num_file)" data_file

Or its much slower but more attractive, and possibly more well-behaved, sibling:

while read a; do echo "${a}p;"; done < line_num_file | xargs -I{} sed -n {} data_file

A variation:

xargs -a line_num_file -I{} sed -n {}p; data_file

You can speed up the xarg versions a little bit by adding the -P option with some large argument like, say, 83 or maybe 419 or even 1177, but 10 seems as good as any.

*xargs --show-limits </dev/null can be instructive

I d investigate Perl, since it has the regexp facilities of sed plus the programming model surrounding it to allow you to read a file line by line, count the lines and extract according to what you want (including from a file of line numbers).

my $row = 1
while (<STDIN>) {
   # capture the line in $_ and check $row against a suitable list.
   $row++;
}

In Perl:

perl -ne  print if $. =~ m/^(1|5|1010|20503)$/  file




相关问题
Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Function to create the array by reading the file

I am creating scripts which will store the contents of pipe delimited file. Each column is stored in a separate array. I then read the information from the arrays and process it. There are 20 pipe ...

Compare characters at the end of the string C++

This program supposed to find command line arguments entered on Unix which ends with “.exe”. For some reason it doesn t work. Here is the code: int main( int argc, char* argv[] ) { for ( int ...

Batch Job Dependencies Using Open Source/Free Software

I run a large data warehouse plant where we have a lot of nightly jobs running concerruently however many have dependencies on a extract or data load process before they start. Currently we use an ...

Writing application for both Unix and Windows

I ll write a program for Interactive UNIX (http://en.wikipedia.org/wiki/INTERACTIVE_UNIX). But in a year it will be ported to Windows. I ll write it in ANSI C and/or SH-script. When it runs on Windows ...

Development Environment in Windows [closed]

What are your recommendations for setting up a development environment in Windows, especially when not using an IDE. I am attempting to familiarize myself with Windows, and I feel a bit lost. What ...

热门标签