MirrG
MirrG

Reputation: 406

Remove elements from a certain positions

I have a file with three columns. From some of the rows I want to delete an element in the third column. I have also a list with number of lines, from which I want to delete the third element.

My file looks like :

2.34 5.3974 8.202847
8.271 6.294 9.04
9.2846 5.847 1.372

My list of line numbers:

2
3

The desire output:

2.34 5.3974 8.202847
8.271 6.294 
9.2846 5.847

When I had a small files, I just used:awk '{if (NR==2 || NR==3) $3=""; print}' file. But this solution doesn't work, if I had ~1500 lines to change. How is it possible to do using bash and awk (or sed)? I would appreciate every suggestions.

Upvotes: 0

Views: 48

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

One solution is that you save the line numbers in a file say lines and use awk to read through that file and the input file to filter out the lines.

Example

$ cat lines
2
3
$ awk 'FNR==NR{line[$0]++; next} FNR in line{$3=""}1' lines file
2.34 5.3974 8.202847
8.271 6.294
9.2846 5.847

Upvotes: 3

Related Questions