New answers tagged

1

Operating perl in the paragraph mode -00 and loop over all the paragraphs one by one -n. First we fill up the type, anticodon, sequence, and str variables by looking at their properties in the current para, aka, $_. $ perl -n00e ' my($type, $anticodon, $seq, $str) = / (?= .*\nType: \h+ (\S+) ) (?= .*\hAnticodon: \h+ (\S+) ) (...


0

One approach can be using perl in paragraph mode and then split the para in newlines. The last line of the para we use to determine our positions and lengths of cordons and then use these numbers to go grab the data from the just preceding line. $ perl -F\\n -l -00 -nae ' $F[-1] =~ /^Str:\s+((.)\2*){7}((.)\4*)/g; my $c = substr($F[-2],pos($F[-1])-...


0

My two cents while IFS= read -r line; do [[ "$(echo $line | grep "Type:")" ]] && codon="$(echo $line | cut -d' ' -f4)" && start="$(echo $line | cut -d' ' -f6)" && type="$(echo $line | cut -d' ' -f2)" && continue [[ "$(echo $line | grep "Seq:")" ]] && seq="${line##Seq: }" && continue if [[ -n "$seq" &&...


2

This is my approach that actually uses these >>.......<< to find the desired sequence, as it was requested in the original question. I started working on it before this shortcut was found. It turned out to be a pleasant exercise with sed (although the approach may be far from optimal). I'm posting it here as an example that sed can do this. <...


2

Assuming that you need to parse the repetitions of the Str string: start and end Since the sequence of patterns could change for each block we need a way to find the 8th pattern. It is possible to extract each repeated "pattern" (from your description anything that starts with a character and stops with same character) from the str with (GNU) grep: $ str=...


5

Using awk: $ awk -f script.awk file Sequence: CTCACAC, Anticodon: CAC, Type: Val Sequence: CTGAAGA, Anticodon: GAA, Type: Phe Sequence: CTGCCAC, Anticodon: GCC, Type: Gly Sequence: TTTACAC, Anticodon: TAC, Type: Val Sequence: CTGATAA, Anticodon: GAT, Type: Ile Sequence: CTGATAA, Anticodon: GAT, Type: Ile Sequence: CTGATAA, Anticodon: GAT, Type: Ile Sequence:...


-1

short example, if I got the question correctly. if FILE.txt contains provided by you data, the output below the string to execute: for I in $(cat FILE.txt|egrep '^Seq:\s|^Str:\s'|sed ':a;N;$!ba;s/Seq:\s*//g;s/\nStr:\s*/|/g;'); do B=$(echo "$I"|cut -d'|' -f1); A=$(echo "$I"|cut -d'|' -f2); [ ${#A} ] && s=${A:0:1} && s1=0 && s2=1 &...


3

Given that we can extract the starting index together with the anticodon: len=7 prior=2 while IFS= read -r line; do if [[ $line =~ Anticodon:" "([[:alpha:]]+)" at "([0-9]+) ]]; then anticodon=${BASH_REMATCH[1]} start=$(( BASH_REMATCH[2] - 1)) # string indexing is zero-based elif [[ $line == "Seq: "* ]]; then seq=${line#Seq:...


0

Now if bash were an option: You could enable extended globbing and combine the @( and !( sub-patterns to construct the glob @(!(*[[:upper:]]*)|!(*[[:lower:]]*)|!(*[[:punct:]]*)|!(*[[:digit:]]*)) to compare against $ shopt -s extglob $ arr=( '!(*'{'[[:upper:]]','[[:lower:]]','[[:punct:]]','[[:digit:]]'}'*)' ) $ pattern=$(IFS='|'; printf '@(%s)' "${arr[*]}") $...


1

Stealing shamelessly from @HaroldFischer @bxm and @RomanPerekhrest for a pure awk solution awk -v test="does not meet" '/[a-z]/ && /[A-Z]/ && /[0-9]/ && /[[:punct:]]/ {test="meets"} END {print "String "test" your requirements"}' <<<"Aa&0"


0

For completeness, since no other answers mention PCRE. A limitation of BRE/ERE is that you cannot trivially¹ implement a logical and for the equivalent logical "or" in alternation with |. PCRE patterns let you create "and" conditions using zero-width assertions: look-ahead or look-behind. These "consume" no characters, but restrict matching before or after ...


0

A basic way to test that characters fall into character ranges (lower, upper, etc) is to use a shell pattern (similar to a glob). password='aA23.sd' res=true for p in lower upper digit punct do if [ "${str}" = "${password#*[[:$p:]]}" ] then res=false; break fi done if "$res" then echo "The password match all ...


3

This is RomanPerekhrest's answer rewritten to work with mawk: #!/bin/sh -- string='Aa1!z' if printf '%s\n' "$string" | LC_ALL=C awk '/[a-z]/ && /[A-Z]/ && /[0-9]/ && /[!-\/:-@[-`{-~]/ {exit 1}'; then printf '%s\n' 'String does not meet your requirements' else printf '%s\n' 'String meets your requirements' fi It also borrows ...


3

Inspired by RomanPerekhrest, but with some minor refinements to do away with the pipeline and command substitution: if awk '/[[:lower:]]/ && /[[:upper:]]/ && /[[:digit:]]/ && /[[:punct:]]/ {exit 1}' <<< "$string" ; then echo "did not match all requirements" else echo "looks good to me" fi


7

With one call to awk and without pipe: #! /bin/sh - string='whatever' has_char_of_each_class() { LC_ALL=C awk -- ' BEGIN { for (i = 2; i < ARGC; i++) if (ARGV[1] !~ "[[:" ARGV[i] ":]]") exit 1 }' "$@" } if has_char_of_each_class "$string" lower upper digit punct; then echo OK else echo not OK fi That's POSIX but note that ...


3

The following script is longer than your code, but shows how you could test a string against a list of patterns. The code detects whether the string matches all patterns or not and prints out a result. #!/bin/sh string=TestString1 failed=false for pattern in '*[[:upper:]]*' '*[[:lower:]]*' '*[[:digit:]]*' '*[[:punct:]]*' do case $string in $...


6

With flexible awk pattern matching: if [[ $(echo "$string" | awk '/[a-z]/ && /[A-Z]/ && /[0-9]/ && /[[:punct:]]/') ]]; then echo "String meets your requirements" else echo "String does not meet your requirements" fi


Top 50 recent answers are included