extract string until regexp from backside

Hi,

I searched in the forums, but I didn't find a good solution. My problem is:
I have a string like "TEST.ABC201005.MONTHLY.D101010203".
I just want to have the string until the D100430, so that the string should look like: "TEST.ABC201005.MONTHLY.D"
The last characters after the D can be 6-10 characters long, so that sometimes D101010 is the last part of the string, but the next day D10301006.

Does anyone know a handy sed?

I tried: echo $string |sed 's/.\{6\}$//g'

Hi,
if then number of fields are always 4 (with . as field separator) maybe this would be a solution:

echo $string | awk 'BEGIN{FS="."} {print $1"."$2"."$3".D"}'

if the number of fields are variable, then use a for loop until NF-1 (number of fields.

1 Like

Try:

echo $string | sed 's/\(.*\..\).*/\1/'
1 Like

hmm..can you explain this ..looks like chinese for me :stuck_out_tongue:

SED sol:

echo "TEST.ABC201005.MONTHLY.D101010090900203"|sed -e "s/\.D[^\.]\{1,\}/\.D/"
1 Like

I tried this:
echo $string |awk -F. '{print $1"."$2"."$3".D"}' - I think this is the same :slight_smile:

---------- Post updated at 11:55 AM ---------- Previous update was at 11:53 AM ----------

It looks like I have to learn a new language - sed. Very confusing.

Have a read of Sed - An Introduction and Tutorial.

This can also be done without sed:

echo ${string%.D*}.D

or sed:

echo $string | sed 's/[0-9]*$//'
STRING="TEST.ABC201005.MONTHLY.D101010203"
echo "${STRING%.*}.D"