Home > front end >  Script linux loop for multiple number
Script linux loop for multiple number

Time:05-14

I have a list of Linux commands that I need to repeat up to 670. How can I automate the commands, so I don't have to do the 670 times. I don't know how to do all the operations from C1, C2, C3 .... to C670?

#START C1


mkdir C1

cd C1

awk '/Prymnesium_parvum_GenomeV1.0_Contig_1\t/' ../Methy_670.txt | cut -f 2 > IllU_C1.txt

awk '/Prymnesium_parvum_GenomeV1.0_Contig_1\t/' ../TE/TEannotation_Prymnesium_parvum_GenomeV1.0.gff3 | cut -f 4,5 > TE_C1.txt

paste TE_C1.txt IllU_C1.txt> C1.txt


awk -f ../com.awk C1.txt C1.txt > res_C1

wc -l res_C1

wc -l TE_C1.txt

cd ..

###Stop C1

#Start C2

mkdir C2

cd C2

awk '/Prymnesium_parvum_GenomeV1.0_Contig_2\t/' ../Methy_670.txt | cut -f 2 > IllU_C1.txt

awk '/Prymnesium_parvum_GenomeV1.0_Contig_2\t/' ../TE/TEannotation_Prymnesium_parvum_GenomeV1.0.gff3 | cut -f 4,5 > TE_C1.txt

....

CodePudding user response:

Not tested:

#!/bin/bash

for C in {1..670};do
echo START ${C}

mkdir -p ${C} && cd ${C}

awk '/Prymnesium_parvum_GenomeV1.0_Contig_'${C}'\t/' ../Methy_670.txt | cut -f 2 > IllU_${C}.txt

awk '/Prymnesium_parvum_GenomeV1.0_Contig_'${C}'\t/' ../TE/TEannotation_Prymnesium_parvum_GenomeV1.0.gff3 | cut -f 4,5 > TE_${C}.txt

paste TE_${C}.txt IllU_${C}.txt> ${C}.txt

awk -f ../com.awk ${C}.txt ${C}.txt > res_${C}

wc -l res_${C}

wc -l TE_${C}.txt

cd ..

echo Stop ${C}

done

I am not sure what awk '/Prymnesium_parvum_GenomeV1.0_Contig_1\t/' ../Methy_670.txt does but you will have to figure out how you can add ${C} in there as single quotes will not parse ${C}

CodePudding user response:

Another take: awk can do what cut does

#!/usr/bin/env bash

prep_dir() {
  local n=$1
  local c=C$n  ill=IllU_$c.txt  te=TE_$c.txt
  local pattern="Prymnesium_parvum_GenomeV1.0_Contig_${n}\t"

  if mkdir "$c" && cd "$c"; then
    awk -v p="$pattern" '$0 ~ p {print $2}' ../Methy_670.txt > "$ill"
    awk -v p="$pattern" '$0 ~ p {print $4, $5}' ../TE/TEannotation_Prymnesium_parvum_GenomeV1.0.gff3 > "$te"

    paste "$te" "$ill" > "$c.txt"
    awk -f ../com.awk "$c.txt" "$c.txt" > "res_$c"
    wc -l "res_$c"
    wc -l "$te"
    cd ..
  fi
}

for i in {1..670}; do
  prep_dir $i
done
  • Related