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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-13-2010, 02:22 AM
|
#1
|
|
Member
Registered: Mar 2006
Distribution: FC4
Posts: 184
Rep:
|
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
|
|
|
|
12-13-2010, 03:38 AM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Use parameter substitution. In particular look at ${var#pattern} and ${var%pattern}. Feel free to ask if in doubt.
|
|
|
|
12-13-2010, 03:42 AM
|
#3
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,711
|
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):
This strips the first part (TT. in your example):
Using bash internals is also faster (no need to start an external program like sed).
Hope this helps.
|
|
|
|
12-13-2010, 04:00 AM
|
#4
|
|
Member
Registered: Mar 2006
Distribution: FC4
Posts: 184
Original Poster
Rep:
|
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
|
|
|
|
12-13-2010, 04:20 AM
|
#5
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,711
|
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.
|
|
|
|
12-13-2010, 04:51 AM
|
#6
|
|
Senior Member
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: Mageia Studio-13.37 Kubuntu.
Posts: 3,098
Rep: 
|
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 04:55 AM.
Reason: I'm a bit slow
|
|
|
|
12-13-2010, 05:09 AM
|
#7
|
|
Member
Registered: Mar 2006
Distribution: FC4
Posts: 184
Original Poster
Rep:
|
Quote:
Originally Posted by druuna
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
|
|
|
|
12-14-2010, 12:10 AM
|
#8
|
|
Member
Registered: Mar 2006
Distribution: FC4
Posts: 184
Original Poster
Rep:
|
Quote:
Originally Posted by GlennsPref
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.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:58 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|