Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to replace specific column of csv file for first 100 rows

Tags:

awk

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..

like image 705
user752590 Avatar asked Apr 18 '13 10:04

user752590


1 Answers

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.

like image 102
jaypal singh Avatar answered Sep 19 '22 21:09

jaypal singh