I'm reformatting a big file with sample metadata. I have a file (let's call it File2) with the group each sample belong to, with one id and pop per line. My idea was to while read over that file and use sed -i
to update each of the samples info. The issue is that sed is not updating the file.
The input file is a .fam
file from plink, in this fashion:
pop id 0 0 0 -9
pop id 0 0 0 -9
pop id 0 0 0 -9
pop id 0 0 0 -9
Right now pop and id are the same, so I want to update the file with File2, but the sed code I normally use for this doesn't seem to work:
while read -r id pop; do sed -i 's/^$id/$pop/' File1.fam; done < File2.txt
I have tried only the sed command without iteration and it works fine. But I have 700 samples and I would dread having to do this one by one.
Why is it not working?
id pop
, and the idea was to iterate over that file and change the first appearance of $id for its $pop.File1.fam
andFile2.txt
, plus the expected output given that input so we can best help you. You say "I have 700 samples" - also clarify if that means 700 lines in one of the files or 700 instances of one or the other of the files or something else.