Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting.

I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the string, using Sed command.

The closest I could get to is the following Sed command.

$ echo "c:\working\html\index.txt.12-12-2009.bkp" | sed 's/.*[.]//'
result -> bkp

but it seems sed works from right to left of the string and so finds the last occurrence of the "." returning bkp.

I am using Cygwin to run these commands.
Sed version is "GNU sed version 4.1.5"
bash version si "GNU bash, version 3.2.49(22)-release (i686-pc-cygwin)"

TIA. imr

Hi.

Sed is not going from right to left, it just matches the longest string it can.

awk is perhaps simpler for this kind of thing:

echo "c:\working\html\index.txt.12-12-2009.bkp" | awk -F. '{print $1 FS $2}' 
c:\working\html\index.txt

is this what you're after:

#  echo "c:\working\html\index.txt.12-12-2009.bkp" | sed 's/\([^\.]*\.[^\.]*\)\..*/\1/'
c:\working\html\index.txt

code:-

sed 's/\(.*\.[^.]*\)\.[^.]*\.[^.]*/\1/'

BR

If I live to be 100 I'll never understand that stuff :wink:

So a simpler one:

echo "c:\working\html\index.txt.123.12-12-2009.bkp" | cut -d. -f-2

Alternatively if the requirement can be stretched so that extension that needs to be cut always starts with a number then this would work too:

bkp=$(echo "c:\working\html\index.txt.12-12-2009.bkp" | sed 's/\.[0-9].*//')

or if the cut off is always .string.string then we can use plain variable expansion:

bkp="c:\working\html\index.txt.12-12-2009.bkp"
echo ${bkp%.*.*}

oops. That wasn't supposed to happen :wink:

Wow, thanks a lot guys... sure did understand the possibilities with shell script.