2

How to replace the 2nd column name in a .csv file with its corresponding file name for all files within a directory? Does anyone know how to do this with shell scripting? sed or awk

input file name CDXV1.csv

Gene,RPKM(26558640 pairs)
ENSTGUG00000013338 (GAPDH),971.678203888
ENSTGUG00000005054 (CAMKV),687.81249397
ENSTGUG00000006651 (ARPP19),634.296191033
ENSTGUG00000002582 (ITM2A),613.756010638

output file name CDXV1.csv (same)

Gene,CDXV1(26558640 pairs)
ENSTGUG00000013338 (GAPDH),971.678203888
ENSTGUG00000005054 (CAMKV),687.81249397
ENSTGUG00000006651 (ARPP19),634.296191033
ENSTGUG00000002582 (ITM2A),613.756010638
1
  • 1
    What did you try for yourself? Post your research attempts even if they aren't successful Commented Jun 8, 2017 at 6:53

2 Answers 2

1

sed solution:

for f in yourdir/*.csv; do sed -i "1s~^\([^,]*\),\([^(]*\)~\1,${f%%.*}~g" "$f"; done

Details:

  • for f in yourdir/*.csv - iterating through csv filenames

  • -i - modify the file in-place

  • 1s - perform substitution only on the 1st line

  • ~ - used as sed subcommand sections separator

  • ^\([^,]*\),\([^(]*\) - capturing the 1st field and 2nd field value (till encountering ()

  • ${f%%.*} - bash variable substitution, truncate right of the first .

Sign up to request clarification or add additional context in comments.

2 Comments

That worked! thanks! Would you mind explaining the command? I'm just getting into this stuff.
@MadzaYasodaraFariasVirgens, yes, see my details
1
awk -F, -v OFS=,  'NR==1{split(FILENAME,a,".");split($2,b,"(");$2= a[1] "(" b[2]}1' CDXV1.csv
Gene,CDXV1(26558640 pairs)
ENSTGUG00000013338 (GAPDH),971.678203888
ENSTGUG00000005054 (CAMKV),687.81249397
ENSTGUG00000006651 (ARPP19),634.296191033
ENSTGUG00000002582 (ITM2A),613.756010638

If your awk support inplace replacement then use -i inplace :

awk -i inplace -F, -v OFS=,  'NR==1{split(FILENAME,a,".");split($2,b,"(");$2= a[1] "(" b[2]}1' *.csv

3 Comments

OP needs to apply this for all .csv files and also someway to modify the file in-place.
Thank you for nice code PS, could you please let me know which version of awk supports this, as I have gawk 4.0.1 and it is not having this option.
@RavinderSingh13 In latest GNU Awk (since 4.1.0 released), it has the option of "inplace" file editing check for more info stackoverflow.com/questions/16529716/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.