-1

I am trying to change some lines of a code using sed by reading another list of 7000+ lines using cat.

I tried to do :

for i in `cat namefile`; do sed 's/OG0000047/$i/g' paml.ctr > paml_$i.ctr; done

My code (paml.ctr):

seqfile = OG0000047.phy  
treefile = OG0000047.treefile     
outfile = OG0000047_results

My list (namefile):

OG0000047
OG0000076
OG0000080
OG0000194
OG0000223
OG0000255
OG0000271

So read the names on the namefile and change OG0000047 to the names, save into a new file. However, of course, my $I in the sed command does not work and I end up with $I in my code instead of the actual names

Does anyone knows how to solve this issue?

user3188922
  • 329
  • 1
  • 3
  • 19

2 Answers2

1

This solution should work.

for i in `cat namefile`; do sed -e "s/OG0000047/$i/g" paml.ctr > maol_$i.ctr;  done
cforler
  • 179
  • 1
  • 7
1

This might work for you (GNU sed and xargs):

cat namefile |xargs -I{} -n1 sh -c "sed 's/OG0000047/{}/g' paml.ctr > paml_{}.ctr"

Use the namefile as arguments for both sed and the new file name.

The -I{} means use {} as a stub for the argument and -n1 means issue the the sh command (sed with redirection) one at a time.

Alternative, using GNU parallel:

parallel "sed 's/OG0000047/{}/g' paml.ctr > paml_{}.ctr" :::: namefile
potong
  • 55,640
  • 6
  • 51
  • 83