Add new column with filename
1
0
Entering edit mode
23 months ago

Hi there,

I have multiple comma-separated files named A.txt, B.txt, ... with the following structure for A.txt:

> ,detected_genes,mean_abund,mean_abund_nz,median_abund,median_abund_nz
> H02_1,0.8377591851582393,3.2743554431006854,3.60999928151245,3.1628119732276407,3.680581204295978
> H15_2,0.9223946784922394,3.9074020797919604,3.8968716734150335,3.9540308755266276,4.182691920839667
> H17_1,0.8480243161094225,4.570136920313126,4.6683507760942895,4.374416758288309,4.497115788409282

My goal is to add a new column at the end of each row (except the header), with the filename of each file, so that it shows

> ,detected_genes,mean_abund,mean_abund_nz,median_abund,median_abund_nz
> H02_1,0.8377591851582393,3.2743554431006854,3.60999928151245,3.1628119732276407,3.680581204295978,A
> H15_2,0.9223946784922394,3.9074020797919604,3.8968716734150335,3.9540308755266276,4.182691920839667,A
> H17_1,0.8480243161094225,4.570136920313126,4.6683507760942895,4.374416758288309,4.497115788409282,A

Can someone help me out? Thanks!

sequence • 446 views
ADD COMMENT
1
Entering edit mode
23 months ago
awk '{print $0 "," FILENAME}' A.txt B.txt C.txt
ADD COMMENT
1
Entering edit mode

I guess it should be:

awk 'NR!=1{$0=$0","FILENAME}1' a.txt

or

awk '{print (NR==1)?$0:$0","FILENAME}' a.txt
ADD REPLY

Login before adding your answer.

Traffic: 2239 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6