sed query ... printing every nth line of text file

sed query ... printing every nth line of text file

Post by Brad Gibs » Wed, 13 Sep 1995 04:00:00



An ultra-naive question ... how can I use sed, or equivalent, to strip out
every 10th line, say, of an enormous text file, and dump it to another new
file?

Thanks in advance for any clues ...

Cheers,
  Brad

------------------------------------------------------------------------------

  Dept. of Astrophysics
  Keble Road                                    PHONE:    +44-(0)1865-273292
  University of Oxford                          FAX:      +44-(0)1865-273390
  Oxford, England                    
  OX1 3RH
                     URL: http://www-astro.physics.ox.ac.uk/~bkg/gibson.html
------------------------------------------------------------------------------

 
 
 

sed query ... printing every nth line of text file

Post by Bunno Li » Wed, 13 Sep 1995 04:00:00


Try this:
cat bigfile | awk '(NR % 10 > 0)' > newfile

 
 
 

sed query ... printing every nth line of text file

Post by Samir Bajaj, Integration Servic » Wed, 13 Sep 1995 04:00:00


Here's one simple solution to pick every 10th line in a file:

  awk '{ print NR, $0 }' myFile | grep '^[0-9]0 '

Can be generalized. Hope that helps.

--Samir

-----------------------------------------------------------------
Samir Bajaj                            Integration Services Group

(415) 507-8534                         San Rafael, CA 94903
-----------------------------------------------------------------

 
 
 

sed query ... printing every nth line of text file

Post by Samir Bajaj, Integration Servic » Wed, 13 Sep 1995 04:00:00


Quote:

> Here's one simple solution to pick every 10th line in a file:

>   awk '{ print NR, $0 }' myFile | grep '^[0-9]0 '

Oops!
  should have been:

    awk '{ print NR, $0 }' myFile | grep '^[0-9]+0 '
                                                ^
                                                |
                                 was missing ---+

--Samir

 
 
 

sed query ... printing every nth line of text file

Post by Randal L. Schwar » Thu, 14 Sep 1995 04:00:00


Samir>     awk '{ print NR, $0 }' myFile | grep '^[0-9]+0 '
Samir>                                                 ^
Samir>                                                 |
Samir>                                  was missing ---+

Warning: this doesn't work on older versions of sed.  Check your
manpage carefully.

Just another Unix hacker (since 1977, and that's what I mean "older" :-),
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

sed query ... printing every nth line of text file

Post by Randal L. Schwar » Thu, 14 Sep 1995 04:00:00


This week's Useless Use of Cat Award goes to

Bunno> Try this:
Bunno> cat bigfile | awk '(NR % 10 > 0)' > newfile

Remember, nearly *anything* of the form:

        cat fred | barney betty wilma ...

can be rewritten as:

        <fred barney betty wilma ...

thus saving a wasted process.  However, in this case, "awk" knows how
to read filenames on the commmand line, so we can dingle it this way
instead:

        awk '(NR % 10 > 0)' bigfile > newfile

Just another Useless Use of Usenet,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.teleport.com/~merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

sed query ... printing every nth line of text file

Post by Sally Woolric » Thu, 14 Sep 1995 04:00:00




Quote:

> An ultra-naive question ... how can I use sed, or equivalent, to strip out
> every 10th line, say, of an enormous text file, and dump it to another new
> file?

> Thanks in advance for any clues ...

'awk', 'nawk' or 'perl' could all do this very easily.

--
============================================================================
Sally Woolrich                    |          This mail contains my personal

============================================================================

 
 
 

1. Insert a line of text every nth line

I am working with a SQL file consisting of 50,000+ INSERT statements.
I would like to insert a COMMIT; statement every 1,000 lines. How can
I accomplish this in ksh using a loop function? Can it be effectively
accomplished in awk?

CURRENTLY:
Line 999  Insert into test (cola, colb, colc) values (1,2,3);
Line 1000 Insert into test (cola, colb, colc) values (4,5,6);
Line 1001 Insert into test (cola, colb, colc) values (7,8,9);

SHOULD BE:
Line 999  Insert into test (cola, colb, colc) values (1,2,3);
Line 1000 Insert into test (cola, colb, colc) values (4,5,6);
Line 1001 COMMIT;
Line 1002 Insert into test (cola, colb, colc) values (7,8,9);

Thanks in advance.

2. User management: disable users to view mounted drives

3. Q:print nth line of file using SED

4. PPP for Xenix

5. Print Nth matching line with sed

6. Fujitsu 141v Monitor

7. sed - printing nth to mth lines

8. Rockwell drivers.

9. printing every other line with sed

10. inserting a blank line after every line - using sed

11. need to split large text file into two, via every-other line

12. How do I select every nth + m record of a file?

13. Printing 1st 200 lines of every file