Command to replace a specific csv file column for the first 100 lines

The following command replaces the second column with the value e in the full csv file, but what if I want to replace only the first 100 rows.

awk -F, '{$2="e";}1' OFS=, file

The remaining lines of the csv file should be intact.

+5
source share
2 answers

awk -F, 'NR<101{$2="e";}1' OFS=, file

NRthe built-in variable gives you either the total number of records processed or the line number depending on usage. In the above awk example, the variable NRhas a line number. When you place the template NR<101, the action will be true for the first 100 lines. When it is false, the default will be 1, which will print the remaining lines as is.

+7
source

try the following:

awk -F, 'NR<=100{$2="e"}1' OFS=, file
+5
source

All Articles