0

I have many files in subdirectories eg. UCE-1…UCE-2000, which all contain the same two file types (a .cfg file and a .phylip file).

UCEs

UCE-13

partition_finder.cfg 

UCE-13.phylip

I need to modify the .cfg file in all of these UCE-1...UCE-2000 folders. Specifically, I need to copy the file name of the .phylip file UCE-13.phylip and place it in a specific section of text inside the .cfg file, for instance change

alignment = ;

to

alignment = UCE-13.phylip;

A second modification I need to make is to copy a section of text always found in the .phylip file at the end of the first line preceded by a space and replace it in a specific location of the .cfg file.

Copy last set of numbers in 1st line of the .phylip file between the space and return

2 466\r 

Then find replace it in .cfg

All = 1-;

to

All = 1-466;

The numbers very in length.

Any help with either of these problems would be greatly appreciated.

2
  • What are the changes you tried. That would help in rectifing things!
    – Deca
    Jun 30, 2016 at 12:01
  • find /directory -name *.confg -exec sed -i "s/All = 1-;/All = 1-some_num;/g" {} \; but am not sure how to get the “2 466\r” from the .phylip file and place it in the right way, sorry new to regular expressions
    – Matt
    Jun 30, 2016 at 12:09

1 Answer 1

0

start in “All-UCEs"
the info is all common to a subdir so
go to that dir first
get the phylip name
get the last field of the first row of the phylip file
stick them in the .cfg file
(use double quote to allow expansion of shell var in sed)
change back out of the dir

for dir in UCE-*; do
    cd ${dir}
    phylip="*.phylip"
    some_num=`awk 'NR==1{print $NF}' ${phylip}`
    sed -i "s/alignment  = ;/alignment  = ${phylip};/;\
        s/All = 1-;/All = 1-${some_num};" *.cfg
    cd ..
done

(untested)

2
  • sorry the file structure is this “All-UCEs/UCE-1/ partition_finder.cfg uce-1.phylip I modified the script some as awk was not finding the .phylip file as well as for sed –i in mac, for dir in uce-; do phylip="${dir}/.phylip" some_num=awk 'NR==1{print $NF}' ${phylip} sed -i '' 's/alignment = ;/alignment = ${phylip};/' ${dir}/*.cfg sed -i '' 's/All = 1-;/All = 1-${some_num};/' ${dir}/*.cfg done Unfortunately it places the text “${phylip}” and “All = 1-${some_num};” instead of the values. Thanks again!
    – Matt
    Jul 1, 2016 at 0:47
  • sorry being redundant with the ${dir} in two places, just cd there ant forget the rest. shell variables do not expand inside single quotes, change to double quotes
    – tomc
    Jul 1, 2016 at 7:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.