0

I am trying to modify a json file using a for loop to loop over a list of sample names and use sed to fix those sample names. Right now a subset of the input file is;

      "WA82671",
        "WA8267-technical-replicate-1":"AGCGTTGAT+CACATGTCC",
        "WA8267-technical-replicate-2":"TCATGGTCAAT+TGCAGTGAGC"
    "WA82671":{ "/scratch/jjz86252/ipyrad/3RAD_processed/WA8267_R1_1.fq.gz",
                "/scratch/jjz86252/ipyrad/3RAD_processed/WA8267_R2_1.fq.gz"
        "name":"WA82671",

I would like it to be;

      "WA8267",
        "WA8267-technical-replicate-1":"AGCGTTGAT+CACATGTCC",
        "WA8267-technical-replicate-2":"TCATGGTCAAT+TGCAGTGAGC"
    "WA8267":{"/scratch/jjz86252/ipyrad/3RAD_processed/WA8267_R1_1.fq.gz", 
              "/scratch/jjz86252/ipyrad/3RAD_processed/WA8267_R2_1.fq.gz"
        "name":"WA8267",

I have tried the follwing command; for i in $(cat samplenames); do sed -er 's/$i\1/$i/g' names; done Howvever, this command fails to make any changes. It simply prints the entire file for each value of i.

  • use sed --in-place to make edits to the file once you're sure that what it's changing is what you want. – DopeGhoti May 8 at 21:15
  • Making a backup of your original file first is a good idea too! – Jeff H. May 9 at 0:03
2

Since you're using file paths that have the / forward slash, you should use commas in your sed expression. Something like...

for name in $(cat samplenames); do
    sed -e "s,${name}1,${name},g" sample --in-place
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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