33

I have a very huge file in which I need to obtain every nth line and print it into a row.

My data:

1      937  4.320194
2      667  4.913314
3      934  1.783326
4      940  -0.299312
5      939  2.309559
6      936  3.229496
7      611  -1.41808
8      608  -1.154019
9      606  2.159683
10     549  0.767828

I want my data to look like this:

1      937  4.320194
3      934  1.783326
5      939  2.309559
7      611  -1.41808
9      606  2.159683

This is of course an example, I want every 10th line for my huge data file. I tried this so far:

 NF == 6 {
     if(NR%10) {print;}
     }
user1269741
  • 437
  • 1
  • 5
  • 7

5 Answers5

64

To print every second line, starting with the first:

awk 'NR%2==1' file.txt

To print every tenth line, starting with the tenth line:

awk 'NR%10==0' file.txt

To use this in a script, add the following to a file called script.awk:

BEGIN {
    print "Processing file"
}

NR%10==0

END {
    print "Finished processing"
}

Then execute:

awk -f script.awk file.txt
Steve
  • 51,466
  • 13
  • 89
  • 103
18

With sed, you can do a lot of variations on this quite easily with the first~step command. For instance:

# Odd lines
sed -n 1~2p file
# Every tenth line (10, 20, 30, ...)
sed -n 10~10p file
# Every tenth line (1, 11, 21, ...)
sed -n 1~10p file
# First plus every tenth (1, 10, 20, 30, ...)
sed -n -e 1p -e 10~10p file
Kevin
  • 53,822
  • 15
  • 101
  • 132
9

Piece of cake: cat test.txt | awk 'NR % 10 == 1'

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • That prints the line following every 10th line - lines 1, 11, 21, 31, etc. You want lines where the modulus is zero instead of 1. – D.Shawley Apr 02 '12 at 09:09
  • 2
    Well, why? This also prints every 10th line, starting with the first, and it matches with what is given as example. – Johan Kotlinski Apr 02 '12 at 11:42
2

Doing it directly in command Prompt (Windows).

Put the gawk.exe file in the folder where the file is and start a command Prompt in the folder, and write

gawk "NR%n==x" oldfile.txt>newfile.txt

n is every n'th line you want to print and x is the starting line.

E.g n=10 and x=1, printing line 1,11,21,31,41......end line from the original file into the new file.

E.g n=20 and x=5, printing line 5,25,45,65......end line from the original file into the new file.

2

It's not (g)awk, but it'll work:

cat myfile | grep ^[[:digit:]]*0[[:blank:]] should do the trick.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125