marcin
marcin

Reputation: 67

Change values in the column

I've got an issue with my feature_vector.txt file.

It looks like:

1,1
2,3
3,3
4,2
etc

I am thinking about using AWK to change the values of the second column to zero(0) from the 3rd row to the eg. 20th row.

So the output looks like:

1,1
2,3
3,0
4,0
etc.

Do you have any suggestions?

Upvotes: 0

Views: 52

Answers (2)

anubhava
anubhava

Reputation: 784998

You can use:

awk -F, -v OFS=, 'NR>=3 && NR<=20 {$2=0} 1' file
  • -F, sets input field separator as comma
  • -v OFS=, will set output field separator as comma
  • NR>=3 && NR<=20 executes a block if current record # is >= 3 and <= 20
  • $2=0 will set 2nd field to 0
  • 1 is default awk action to print the values

Upvotes: 3

fredtantini
fredtantini

Reputation: 16556

Using awk:

~$ awk -F',' '(NR>3 && NR<6){print $1",0"} (NR<=3||NR>=6)' f
1,1
2,2
3,3
4,0
5,0
6,6
7,7
8,8
9,9
10,10

using sed:

~$ sed '3,6 s/,.*/,0/' f
1,1
2,2
3,0
4,0
5,0
6,0
7,7
8,8
9,9
10,10

Upvotes: 1

Related Questions