
私はbashの初心者であり、フレーズリストを繰り返そうとしています。私の目標は次のとおりです。
A)以下を.
使用して各構文を分割します。
B)元のフレーズも使用できます。
私の疑似コード/試行は次のとおりです -
while read x
do
eval "whole_phrase=$x" # store the whole phrase to another variable
eval "first_element=echo $x | cut -d';' -f1" # extract the first element after splitting
myprogram -i ../$first_element -o ../$whole_phrase
done < ListOfDotSeparatedPhrases.txt
ListOfDotSeparatedPhrases.txt
それは次のとおりです -
18T3L.fastqAligned.sortedByCoord.out.bam
35T10R.fastqAligned.sortedByCoord.out.bam
18T6L.fastqAligned.sortedByCoord.out.bam
40T4LAligned.sortedByCoord.out.bam
22T10L.fastqAligned.sortedByCoord.out.bam
38T7L.fastqAligned.sortedByCoord.out.bam
私はこれを行うための最良の方法をウェブ上で検索しようとしましたが、失敗しました。どんなアイデアがありますか?私はこれが実際にそれほど難しくないと信じています!
答え1
read
分割してみましょう。フィールド区切り文字を設定するにはどうですか.
?
while IFS=. read -r first_element remainder; do
echo myprogram -i "../$first_element" -o "../${first_element}.${remainder}"
done < ListOfDotSeparatedPhrases.txt
myprogram -i ../18T3L -o ../18T3L.fastqAligned.sortedByCoord.out.bam
myprogram -i ../35T10R -o ../35T10R.fastqAligned.sortedByCoord.out.bam
myprogram -i ../18T6L -o ../18T6L.fastqAligned.sortedByCoord.out.bam
myprogram -i ../40T4LAligned -o ../40T4LAligned.sortedByCoord.out.bam
myprogram -i ../22T10L -o ../22T10L.fastqAligned.sortedByCoord.out.bam
myprogram -i ../38T7L -o ../38T7L.fastqAligned.sortedByCoord.out.bam
からman bash
:
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p
prompt] [-t timeout] [-u fd] [name ...]
One line is read from the standard input, or from the file
descriptor fd supplied as an argument to the -u option, split
into words as described above under Word Splitting, and the
first word is assigned to the first name, the second word to the
second name, and so on. If there are more words than names, the
remaining words and their intervening delimiters are assigned to
the last name. If there are fewer words read from the input
stream than names, the remaining names are assigned empty val‐
ues. The characters in IFS are used to split the line into
words using the same rules the shell uses for expansion
(described above under Word Splitting).
または(実際にはこれがより簡単で移植性が優れています)、行全体を読み取って保持し、シェル引数拡張を使用して残りの部分を削除して最初の要素を作成します。
while read -r x; do
myprogram -i "../${x%%.*}" -o "../$x"
done < ListOfDotSeparatedPhrases.txt
答え2
一方:
eval "whole_phrase=$x" # store the whole phrase to another variable
より良い点は次のとおりです。
whole_phrase="$x"
そして以下を提供します:
eval "first_element=echo $x | cut -d';' -f1" # extract the first element after splitting
最初の要素を抽出する方法はいくつかあります。
区切り文字はピリオドまたはそのため、.
これをに渡してawk
最初のフィールドのみを印刷するように要求します。
first_element="$(awk -F. '{print $1}' <<< "$x")"
または、この特別な場合は最初の要素のみが必要なので、最初のsed
文字.
とその後のすべての項目を簡単に削除できます。
first_element="$(sed -e 's/\..*//' <<< "$x")"
x
最後に、ファイルから読み取った変数を変更しない限り、すでにwhole_phrase
値があることを考えてみましょう。実際にはループ内でこの変数名を使用できますwhile
。
while read whole_phrase
do
first_element="$(awk -F. '{print $1}' <<< "$whole_phrase")"
myprogram -i "../$first_element" -o "../$whole_phrase"
done < ListOfDotSeparatedPhrases.txt