Issue
I am trying to fit sed to my use case I have a list of string all starting with FP_ or DEV_ .... and ending with some numbers I need to find all the items where they start with Dev_ and ends with numbers of 3 digits or less so for example this one wont be accepted then
Dev_xxxxx-1111
but this is accepted and should be kept in the list
Dev_xxxxx-111
I am trying this
sed -e 's/ ",'${IMAGETAG}/Dev/
which gives me all the string starting with Dev and that fix half of the issue but cannot filter out and exclude the ones ends with more than 3 digits so here is what I need here is my input
[ "1.3.0.0-amd64", "Dev_04938095cc4bcefb1f85d7b306c1c49d335e80ce_167075", "Dev_08346f824a146dafbdca4b63eb3ce0b1549c6346_165", "Dev_0ab8997f5d45ed6b7515b5148d492c98109bf94a_16"]
and output should be
[ "Dev_08346f824a146dafbdca4b63eb3ce0b1549c6346_165", "Dev_0ab8997f5d45ed6b7515b5148d492c98109bf94a_16"]
any help is appreciated
Solution
If your strings are for example in a file, you can use grep
instead of sed to match the whole line using
grep -E "^(Fp|Dev)_(.*[^0-9])?[0-9]{1,3}$" file
See the regex matches here.
An example in bash matching anything between 2 underscores and ending with 1-3 digits:
#!/bin/bash
arr=("1.3.0.0-amd64" "Dev_04938095cc4bcefb1f85d7b306c1c49d335e80ce_167075" "Dev_08346f824a146dafbdca4b63eb3ce0b1549c6346_165" "Dev_0ab8997f5d45ed6b7515b5148d492c98109bf94a_16")
for i in "${arr[@]}"
do
if [[ $i =~ ^(Fp|Dev)_.*_[0-9]{1,3}$ ]]; then
echo "Match: $i"
else
echo "No match: $i"
fi
done
Output
No match: 1.3.0.0-amd64
No match: Dev_04938095cc4bcefb1f85d7b306c1c49d335e80ce_167075
Match: Dev_08346f824a146dafbdca4b63eb3ce0b1549c6346_165
Match: Dev_0ab8997f5d45ed6b7515b5148d492c98109bf94a_16
See the regex matches here.
Answered By - The fourth bird Answer Checked By - Senaida (WPSolving Volunteer)