LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   remove particular characters using sed (https://www.linuxquestions.org/questions/linux-software-2/remove-particular-characters-using-sed-849861/)

dsids 12-13-2010 02:22 AM

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

colucix 12-13-2010 03:38 AM

Use parameter substitution. In particular look at ${var#pattern} and ${var%pattern}. Feel free to ask if in doubt.

druuna 12-13-2010 03:42 AM

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.

dsids 12-13-2010 04:00 AM

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

druuna 12-13-2010 04:20 AM

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.

GlennsPref 12-13-2010 04:51 AM

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>

dsids 12-13-2010 05:09 AM

Quote:

Originally Posted by druuna (Post 4189606)
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

dsids 12-14-2010 12:10 AM

Quote:

Originally Posted by GlennsPref (Post 4189622)
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


All times are GMT -5. The time now is 07:14 AM.