My strings are:
In bash when I run:
echo "TESTING_ABC_1-JAN-2022.BCK-gz;1" | sed 's/.*\([0-9]\{1,2\}-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9]\).*/\1/' it returns 1-JAN-2022 which is good.
But when I run:
echo "TESTING_ABC_30-JAN-2022.BCK-gz;1" | sed 's/.*\([0-9]\{1,2\}-[A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9]\).*/\1/' I get 0-JAN-2022 but I want 30-JAN-2022.
From me passing in my string. How can I do it so that I can get single or double digit dates in one line like "30-JAN-2022" or "1-JAN-2022"
1st solution: With your shown samples please try following awk code. Simply using gsub function of awk to globally substitute from starting of value to till 2nd occurrence of - AND from dot to till last of value with NULL and printing remaining of the value of current line.
awk '{gsub(/^[^_]*_[^_]*_|\..*/,"")} 1' Input_file
2nd solution: Using GNU grep please try following code. Using -oP options to print matched value and enabling PCRE regex with these options respectively. In main program using .*? concept for non-greedy match and using it 2 times from starting of value to match till 2nd occurrence of _ and then using \K option to forget all matched values. After that matching values just before . occurrence to get required output.
grep -oP '^(.*?_){2}\K[^.]*' Input_file
3rd solution: Using GNU awk with its match function which is using array concept of creating its indexes(keys) from matched regex's capturing groups.
awk 'match($0,/^[^_]*_[^_]*_([^.]*)/,arr){print arr[1]}' Input_file
4th solution: Using sed with -E option to enable ERE(extended regular expressions) and using regex ^[^_]*_[^_]*_([^.]*).* to create 1 and only capturing group which contains required value in it and using substitute function to substitute whole value with only captured value.
sed -E 's/^[^_]*_[^_]*_([^.]*).*/\1/' Input_file
5th solution: Using GNU grep please try following, thanks to "the fourth bird" for this one.
grep -oP '^(?:[^_]*_){2}\K[^.]*' Input_file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With