Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete column

Status
Not open for further replies.

ovince

Programmer
Feb 27, 2007
55
FR
hi All,

I would like to delete 2 columns from 60 columns data. How to do that except to write

awk '{print $1, $2 .... $60}' file.dat > newFile.dat


leaving out columns that I dont want in a newFile.dat?


thanks
oliver
 
Hi

If you do not care about the separators which will remain in place :
Code:
awk '{$[green][i]40[/i][/green]=$[green][i]50[/i][/green]=""}1' file.dat > newFile.dat
Or a less nice code with nicer output, although all field separators will be set to [tt]OFS[/tt] :
Code:
awk '{for(i=1;i<=NF;i++)if(i!=[green][i]40[/i][/green]&&i!=[green][i]50[/i][/green])printf$i OFS;print""}' file.dat > newFile.dat
Or we just can modify abit the first code to replace multiple field separators with one :
Code:
awk '{$[green][i]40[/i][/green]=$[green][i]50[/i][/green]="";gsub(FS "+",FS)}1' file.dat > newFile.dat
In my examples I removed columns 40 and 50.

Feherke.
 
Unless you have to use awk, perhaps consider using cut, e.g...
Code:
cut -f-39,41-49,51- file.dat > newFile.dat
 
thanks


I have a csv file and I would like to keep like that. This try does not work if I want to remuve 1 column (i.e it leaves ',' in front of lines)

awk 'BEGIN{FS=",";OFS=","}{$1=$6="";gsub(FS "+",FS)}1' file.dat > newFile.dat



Feherke's suggestion is OK




 
sory...Ygor's suggestion with cut works and

awk 'BEGIN{FS=",";OFS=","}{$1=$6="";gsub(FS "+",FS)}1' file.dat > newFile.dat

leave ',' in front of each line
 
Hi

Right. I just wondered how could it be possible. :)

I completely ignored the situation of removing the first or the last field. For those the shell itself is enough.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top