sed regex pour correspondre à + 2-4 chiffres

J’essaie de faire du traitement de texte conditionnel sous Unix et de la syntaxe. Je veux réussir

Find the first 2, 3 or 4 digits in the ssortingng if 2 characters before the found digits are 'WR' (could also be lower case) Variable = the ssortingng we've found (eg WR1234) Type = "work request" else if 2 characters before the found digits are 'RN' (could also be lower case) Variable = the ssortingng we've found (eg RN1234) Type = "release note" else Variable = "WR" + the ssortingng we've found (Prepend 'WR' to the digits) Type = "Work request" fi fi 

Je le fais dans un shell Bash sur Red Hat Enterprise Linux Server version 5.5 (Tikanga)

Merci d’avance, Karl

Je ne sais pas comment vous lisez dans vos cordes, mais cet exemple devrait vous aider à y parvenir. Je boucle sur 4 exemples de chaînes, WR1234 RN456 7890 PQ2342 . Vous n’avez pas dit quoi faire si la chaîne ne correspond pas à votre format attendu ( PQ2342 dans mon exemple), donc mon code l’ignore.

 #!/bin/bash for ssortingng in "WR1234 - Work Request Name.doc" "RN5678 - Release Note.doc"; do [[ $ssortingng =~ ^([^0-9]*)([0-9]*).*$ ]] case ${BASH_REMATCH[1]} in "WR") var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" type="work request" echo -e "$var\t-- $type" ;; "RN") var="${BASH_REMATCH[1]}${BASH_REMATCH[2]}" type="release note" echo -e "$var\t-- $type" ;; "") var="WR${BASH_REMATCH[2]}" type="work request" echo -e "$var\t-- $type" ;; esac done 

Sortie

 $ ./rematch.sh WR1234 -- work request RN5678 -- release note 

J’aime utiliser perl -pe au lieu de sed car PERL a des expressions régulières aussi expressives. Ce qui suit est un peu verbeux pour des raisons d’instruction.

example.txt :

 WR1234 - Work Request name.doc RN456 rn456 WR7890 - Something else.doc wr789 2456 

script.sh :

 #! /bin/bash # search for 'WR' or 'RN' followed by 2-4 digits and anything else, but capture # just the part we care about records="`perl -pe 's/^((WR|RN)([\d]{2,4})).*/\1/i' example.txt`" # now that you've filtered out the records, you can do something like replace # WR's with 'work request' work_requests="`echo \"$records\" | perl -pe 's/wr/work request /ig' | perl -pe 's/rn/release note /ig'`" # or add 'WR' to lines w/oa listing work_requests="`echo \"$work_requests\" | perl -pe 's/^(\d)/work request \1/'`" # or make all of them uppercase records_upper=`echo $records | tr '[:lower:]' '[:upper:]'` # or count WR's wr_count=`echo "$records" | grep -i wr | wc -l` echo count $wr_count echo "$work_requests" 
 #!/bin/bash ssortingng="RN12344 - Work Request Name.doc" echo "$ssortingng" | gawk --re-interval ' { if(match ($0,/(..)[0-9]{4}\>/,a ) ){ if (a[1]=="WR"){ type="Work release" }else if ( a[1] == "RN" ){ type = "Release Notes" } print type } }'