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.

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

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .