Skip to main content
deleted 40 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NRn=NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NRn=NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the thea line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{n=(NRn=NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the the line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=NR-1} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=NR-1}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line. We then increment x by n/9 and print a line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{n=NR-1} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101
deleted 5 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the the line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{incr=n=(NR-1)/9} FNR>x{x+=incr;print} END|| FNR==n{printx+=n/9;print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the the line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{incr=(NR-1)/9} FNR>x{x+=incr;print} END{print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the the line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101
added 34 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

 awk 'FNR==NR{next} FNR==1{incr=n=(NR-1)/9} FNR>x{x+=incr;print} END|| FNR==n{printx+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{incr=n=(NR-1)/9}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We will want to print one line everysave this number as incrn number of lines.

  • FNR>x || FNR==n{x+=incr;printx+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr.   We then increment incrx lines later, we willby n/9 and print anotherthe the line the next time that FNR>x and so on.

  •  

    The condition END{print}FNR==n

    We print assures that the last line alsoin the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{incr=(NR-1)/9} FNR>x{x+=incr;print} END{print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

 awk 'FNR==NR{next} FNR==1{incr=(NR-1)/9} FNR>x{x+=incr;print} END{print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{incr=(NR-1)/9}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We will want to print one line every incr number of lines.

  • FNR>x{x+=incr;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr.  incr lines later, we will print another and so on.

  •  

    END{print}

    We print the last line also.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{incr=(NR-1)/9} FNR>x{x+=incr;print} END{print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101

This answer assumes that your csv file has one line per row, meaning that there are no continued lines. If the file is called db.csv, then you can get 10 lines, including the first and the last by using:

awk 'FNR==NR{next} FNR==1{n=(NR-1)} FNR>x || FNR==n{x+=n/9;print}' db.csv db.csv

How it works

Because the csv file is listed twice on the command line, awk will read through it twice. The first is used to get the total number of lines. The second time is used to print the 10 selected lines.

  • FNR==NR{next}

    NR is the total number of records (lines) read so far.The file record number, FNR, is equal to the total number of records (lines) read so far from this file. So, when FNR==NR, we are still reading the first file. If so, we just jump to the next record.

  • FNR==1{n=(NR-1)}

    If we reach this command, that means that we are on the first line of the second read through. In that case, we know that the total number of records in the file is NR-1. We save this number as n.

  • FNR>x || FNR==n{x+=n/9;print}

    One the first line of the second read through, FNR==1 and x=0. Thus, FNR>x and we print that line and increment x by incr. We then increment x by n/9 and print the the line the next time that FNR>x and so on.

    The condition FNR==n assures that the last line in the file will be printed.

Example

Let us create a file with 101 lines:

$ seq 101 >db.csv

Now, we can use our awk command to print 10 lines from the file, including the first and last:

$ awk 'FNR==NR{next} FNR==1{incr=(NR-1)/9} FNR>x{x+=incr;print} END{print}' db.csv db.csv
1
12
23
34
45
57
68
79
90
101
added 1068 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading