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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed to print a string until the second occurrence of a character
# 1  
Old 11-01-2009
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
# 2  
Old 11-01-2009
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:

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

# 3  
Old 11-01-2009
is this what you're after:

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

# 4  
Old 11-01-2009
code:-

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

BR
# 5  
Old 11-01-2009
If I live to be 100 I'll never understand that stuff ;-)

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

# 6  
Old 11-01-2009
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:
Code:
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:
Code:
bkp="c:\working\html\index.txt.12-12-2009.bkp"
echo ${bkp%.*.*}


Last edited by Scrutinizer; 11-01-2009 at 09:07 AM..
# 7  
Old 11-01-2009
oops. That wasn't supposed to happen ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

3. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

4. Shell Programming and Scripting

Remove last occurrence of character (_) and rest of the string in UNIX (sed)

Hi I need help on this ..!! Input : xx_abc_regA xx_def_regB xx_qwe_regC Now i required the output as the below abc def qwe Need to remove last occurrence of character (_) and rest of the string in Unix (sed). Thanks in Advance ..!!! -Nallachand (3 Replies)
Discussion started by: Nallachand
3 Replies

5. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

8. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

9. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

10. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies
Login or Register to Ask a Question