5

Following command is replacing second column with value e in a complete csv file, but what if i want to replace only in first 100 rows.

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

Rest of the rows of csv file should be intact..

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user752590
  • 111
  • 5
  • 12
  • 29

2 Answers2

7

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

NR built-in variable gives you either the total number of records being processed or line number depending on the usage. In the above awk example, NR variable has line number. When you put the pattern NR<101 the action will become true for first 100 lines. Once it is false, it will default to 1 which will print remaining lines as-is.

jaypal singh
  • 74,723
  • 23
  • 102
  • 147
5

try this:

awk -F, 'NR<=100{$2="e"}1' OFS=, file
Kent
  • 189,393
  • 32
  • 233
  • 301
  • 1
    Or even FNR could be used if multiple files are given as argument. But the result will be one big stream. – TrueY Apr 18 '13 at 11:06
  • How can I specify that I want to append e to only some rows and only some columns? For example I want to add e to columns greater than 10 in row 1. – discipulus Nov 15 '13 at 10:16