LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-13-2010, 03:22 AM   #1
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Rep: Reputation: 31
remove particular characters using sed


Hi,

file = TT.ParlayX_RequestLog_78653_20101212180044.log.17490

1. Want to remove the characters before the first dot (.) including the dot (.)
2. Want to remove the characters after the last dot (.) including the dot (.)

That is, basically, I want the output as:

ParlayX_RequestLog_78653_20101212180044.log


Please advice

Thanks
 
Old 12-13-2010, 04:38 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Use parameter substitution. In particular look at ${var#pattern} and ${var%pattern}. Feel free to ask if in doubt.
 
Old 12-13-2010, 04:42 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Is a sed solution really needed? Bash has an elegant way to do this:

Assuming the file name is stored in a variable (XXX in this example). This strips the last part (.17490 in your example):
Code:
echo ${XXX%.*}
This strips the first part (TT. in your example):
Code:
echo ${XXX#*.}
Using bash internals is also faster (no need to start an external program like sed).

Hope this helps.
 
Old 12-13-2010, 05:00 AM   #4
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Thanks colucix..I'll look into that and if in doubt I'll get back to you...

Thanks druuna..that was fast and short...


I tried this:

echo TT.ParlayX_RequestLog_78653_20101212180044.log.17490 | sed "s/^TT\.//" file | sed 's/\.[0-9][0-9][0-9][0-9][0-9]//'

How can I club the last two seds together and any other way via sed to achieve the above
 
Old 12-13-2010, 05:20 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

To string sed statements together:

sed -e 's/x/y/' -e 's/a/b/' file

Your command would look like this:

Code:
echo "TT.ParlayX_RequestLog_78653_20101212180044.log.17490" | sed -e 's/^TT\.//' -e 's/\.[0-9][0-9][0-9][0-9][0-9]//'
Hope this helps.
 
Old 12-13-2010, 05:51 AM   #6
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Mageia Studio-13.37 Kubuntu.
Posts: 3,325
Blog Entries: 33

Rep: Reputation: 199Reputation: 199
http://sed.sourceforge.net/sed1line.txt

# substitute (find and replace) "foo" with "bar" on each line

This is for a line of text in a file

This may be more useful, have a read of this forum page and use it as reference for what you want to do.

http://programming.itags.org/unix-li...ramming/82536/

This is closer to what you need, but you'l still need the bash interpretation of blocks of strings.

http://www.linuxquestions.org/questi...-files-723311/

http://www.ibm.com/developerworks/ai.../section5.html one of my favourites...

Changing file extensions, but you still need to define the ($)chars, like $1 is the first part of a line, $2 is the second, etcetera.

But you can also define the beginning of a line and the ending of a line without knowing how many parts there are to the line.

you could use a bash script, with a for loop...

Code:
for i in *.txt; { mv $i $i.doc}
would rename files *.txt to *.doc

Hope this gives you food for thought, I tried to search for a real solution, but did not find one.

Therefore I returned to the man pages and google.

Cheers Glenn

<edit> I'm a bit slow, and still learning sed grep and awk, I think the above examples look good. </edit>

Last edited by GlennsPref; 12-13-2010 at 05:55 AM. Reason: I'm a bit slow
 
Old 12-13-2010, 06:09 AM   #7
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Smile

Quote:
Originally Posted by druuna View Post
Hi,

To string sed statements together:

sed -e 's/x/y/' -e 's/a/b/' file

Your command would look like this:

Code:
echo "TT.ParlayX_RequestLog_78653_20101212180044.log.17490" | sed -e 's/^TT\.//' -e 's/\.[0-9][0-9][0-9][0-9][0-9]//'
Hope this helps.
Thanks a lot
 
Old 12-14-2010, 01:10 AM   #8
dsids
Member
 
Registered: Mar 2006
Distribution: FC4
Posts: 184

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by GlennsPref View Post
http://sed.sourceforge.net/sed1line.txt

# substitute (find and replace) "foo" with "bar" on each line

This is for a line of text in a file

This may be more useful, have a read of this forum page and use it as reference for what you want to do.

http://programming.itags.org/unix-li...ramming/82536/

This is closer to what you need, but you'l still need the bash interpretation of blocks of strings.

http://www.linuxquestions.org/questi...-files-723311/

http://www.ibm.com/developerworks/ai.../section5.html one of my favourites...

Changing file extensions, but you still need to define the ($)chars, like $1 is the first part of a line, $2 is the second, etcetera.

But you can also define the beginning of a line and the ending of a line without knowing how many parts there are to the line.

you could use a bash script, with a for loop...

Code:
for i in *.txt; { mv $i $i.doc}
would rename files *.txt to *.doc

Hope this gives you food for thought, I tried to search for a real solution, but did not find one.

Therefore I returned to the man pages and google.

Cheers Glenn

<edit> I'm a bit slow, and still learning sed grep and awk, I think the above examples look good. </edit>

thanks a lot for your help
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 5 07-28-2014 07:25 AM
[SOLVED] Sed, how do I match even characters only? trist007 Linux - Newbie 3 09-03-2010 08:11 PM
Using sed to remove all but the last 17 characters on a line simplified Programming 5 06-04-2010 04:33 AM
Replacing Characters with sed zokken Programming 9 12-02-2009 08:34 PM
using sed to grab two characters? nausicaa3000 Programming 4 04-20-2004 12:01 PM


All times are GMT -5. The time now is 09:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration