Извлечение элементов, разделенных разделителями, с сохранением переменной цикла.

Чтобы создать каталог в ranger , просто введите

:mkdir exampledir

или,

:touch examplefile

0
10.07.2019, 00:31
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
1
28.01.2020, 02:22

Как насчет того, чтобы позволить 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
28.01.2020, 02:22

Теги

Похожие вопросы