Home > database >  Loop through a list both as a variable and as a string
Loop through a list both as a variable and as a string

Time:10-05

I have a long text file from which I want to grep all the lines that are with a given id (id123), and save them in a new text file.

In order to to this I can just execute the command:

zgrep -Hx id123 *.vcf.gz > /home/Roy/id123.txt

However, I have not one, but actually 20 files, so I would need to write this command 20 times. These files are big and they take a long time to be processed, so I would prefer have it running in the background.

That's why I would like to create a script that iterates through a list of the ids, specifying it in the command

For example purposes, something like:

list=("id123" "id124" "id125" "id126")

for i in "${list[@]}"
do
   zgrep -Hx $i *.vcf.gz > /home/Roy/$i.txt
done

CodePudding user response:

How about using awk for dispatching the output of zgrep?

#!/bin/bash

ids=(id123 id124 id125 id126)

zgrep -Hxf <(printf '%s\n' "${ids[@]}") *.vcf.gz |
awk -F ':' '{print > ("/home/Roy/" $NF ".txt")}'

CodePudding user response:

#!/bin/sh -x

find . | sed -n '/id[0-9][0-9][0-9].txt/' > stack

cat > ed1 <<EOF
1p
q
EOF

cat > ed2 <<EOF
1d
wq
EOF

next () {
[[ -s stack ]] && main
exit 0
}

main () {
line=$(ed -s stack < ed1)
zgrep -Hx "${line}" *.vcf.gz > /home/Roy/"${line}".txt
ed -s stack < ed2
next
}

next
  •  Tags:  
  • bash
  • Related