user90
user90

Reputation: 381

Remove first column without removing header

I have a large tab delimited text file like this:

Item Label1 Label2 Label3 
1    Pro01  cake   wine   Toy    
2    Pro02  milk   curd   butter
3    Pro03  rice   basum  white
4    Pro04  tomma  onion  root

I just want to remove the contents from the first column and replace it with the second column without removing the header.

Item  Label1 Label2 Label3 
Pro01  cake   wine   Toy    
Pro02  milk   curd   butter
Pro03  rice   basum  white
Pro04  tomma  onion  root

Upvotes: 0

Views: 463

Answers (3)

Ed Morton
Ed Morton

Reputation: 203229

All you need is:

awk 'NR>1{sub(/[^\t]*\t/,"")}1' file

Upvotes: 3

kvantour
kvantour

Reputation: 26471

Similar to the other solution:

awk '{print (NR>1?substr($0,1+index($0,"\t")):$0)}' file

Upvotes: 2

accdias
accdias

Reputation: 5372

As I said in my comment, you can simply do:

awk 'BEGIN{FS="\t";OFS="\t"}NR==1{print}NR>1{$1=""; print substr($0,2)}' file.txt

You probably want to save the output to a new file as well:

awk 'BEGIN{FS="\t";OFS="\t"}NR==1{print}NR>1{$1=""; print substr($0,2)}' file.txt > newfile.txt

And here is the command above in a structured way for better readability:

awk '

BEGIN {
    FS="\t";
    OFS="\t"
}
NR==1 {
    print
}
NR>1 {
    $1=""; 
    print substr($0,2)
}' file.txt > newfile.txt

Upvotes: 0

Related Questions