0

I have a file samples_long.20Bids.txt with content like this:

P2_305_USD16089489L_HJJNWDSXX_L4
P2_307_USD16089490L_HJNMNDSXX_L3
P2_42_USD16089409L_HJM27DSXX_L1
P2_43_USD16089410L_HJM27DSXX_L1
P2_44_USD16089411L_HJM27DSXX_L1
P2_49_USD16089412L_HJM27DSXX_L1
P2_52_USD16089413L_HJM27DSXX_L1
P2_54_USD16089414L_HJM27DSXX_L1
P2_55_USD16089415L_HJM27DSXX_L1
P2_57_USD16089416L_HJM27DSXX_L1
P2_65_USD16089419L_HJM27DSXX_L1
P2_67_USD16089421L_HJM27DSXX_L2
P2_69_USD16089422L_HJM27DSXX_L2
P2_76_USD16089424L_HJM27DSXX_L2
P2_81_USD16089426L_HJM27DSXX_L2
P2_84_USD16089427L_HJM27DSXX_L2
P2_87_USD16089428L_HJM27DSXX_L2
P2_88_USD16089429L_HJM27DSXX_L2
P2_89_USD16089430L_HJM27DSXX_L2
P2_90_USD16089431L_HJM27DSXX_L2

What I am trying to do is to create many symbolic links in just one command. Thus, I'm using awk for it in that way:

awk '{sample=$1; gsub(/_USD.*/,"",sample); print "/disk1/results/alignment/"sample"/"$1"_bowtie2_sorted.bam"}' /disk1/data/samples_long.20Bids.txt |
 ln -s `awk '{print $0}'` `awk -F "/" '{print $7}'`

However I'm getting this error:

ln: target '/disk1/results/alignment/P2_90/P2_90_USD16089431L_HJM27DSXX_L2_bowtie2_sorted.bam' is not a directory

Even though, I can create the symbolic link individually, eg.:

ln -s /disk1/results/alignment/P2_90/P2_90_USD16089431L_HJM27DSXX_L2_bowtie2_sorted.bam P2_90_USD16089431L_HJM27DSXX_L2_bowtie2_sorted.bam

And no errors launched...

P2_90_USD16089431L_HJM27DSXX_L2_bowtie2_sorted.bam -> /disk1/results/alignment/P2_90/P2_90_USD16089431L_HJM27DSXX_L2_bowtie2_sorted.bam

What am I doing wrong? Or what is the correct way to do it?

Thank you!

UPDATE

Thank to the excellent @Gilles's answer I could do it:

while IFS= read -r line; 
    do sample=$(echo $line | sed 's/_USD.*//g'); 
    ln -s "/disk1/results/alignment/"$sample"/"$line"_bowtie2_sorted.bam" 
    $line"_bowtie2_sorted.bam"; 
done < /disk1/data/samples_long.20Bids.txt

I had to edit a bit the answer because my *_bowtie2_sorted.bam files are saved in subdirectories named by a substring of each line in my file samples_long.20Bids.txt eg. P2_90. However, it is not clear for my why my first code witk awk doesn't work.

1 Answers1

2

If I understand correctly, you have a file samples_long.20Bids.txt containing names, with one name per line. You have a directory containing files (or that will later contain files). You want to create symbolic links from another directory to that directory, with the same base name for the files.

This is easy with a shell loop. (See Understanding "IFS= read -r line" for a detailed explanation of how that loop works. The simple explanation is that it the loop body is executed once for each line in the file.)

while IFS= read -r line; do
  ln -s "/disk1/results/alignment/P2_90/$line" .
done <samples_long.20Bids.txt

I'm not going to explain what you did wrong with your awk code because I have no idea what you meant to do.