[sed] extract words from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [sed] extract words from a string
# 1  
Old 03-12-2010
[sed] extract words from a string

Hi,

One of the scripts creates logs in the format:
progname_file1.log.20100312020657

where after file the number could be from 1 to 28 and after log. the date is attached in the format YYYYMMDDHHMISS

progname_file<1-28>.log.YYYYMMDDHHMISS.

Now I want to discard the .20100312020657 part from the string. Can your give me a command using sed with regular expressions that can to this?

i/p-progname_file1.log.20100312020657
o/p-progname_file1

Thanks in advance.

-dips
# 2  
Old 03-12-2010
You try the following code.

Code:
sed -r 's/(.*[1-28])\.log\..*/\1/' <filename>

# 3  
Old 03-12-2010
Code:
$ echo progname_file1.log.20100312020657|sed 's/\.[0-9].*//'
progname_file1.log

$ echo progname_file1.log.20100312020657|sed 's/\..*//'
progname_file1

# 4  
Old 03-12-2010
Code:
> filename=progname_file1.log.20100312020657
> echo ${filename%.*}
progname_file1.log

# 5  
Old 03-12-2010
Scrutinizer & Franklin52 both of your codes worked.
Vivekraj your code drops ".log" also.

Thanks to all of you for your time. One more request - Can anyone of you send me a link to understand the use of regular expressions with sed? I really want to learn. I have encountered two links in this forum:
1) Sed - An Introduction and Tutorial - doesn't cover regular expressions with sed totally.
2) SED and Regular Expressions - its a bit tough to comprehend.

-dips
# 6  
Old 03-12-2010
sed_tutorial

sed

Please go through the above URL

Last edited by thillai_selvan; 03-12-2010 at 05:27 AM.. Reason: Added another URL
# 7  
Old 03-12-2010
Hi dips_apg,I have dropped .log because you have given some sample input and output.If you want it,try this.

Code:
sed -r 's/(.*[1-28]\.log)\..*/\1/' <filename>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

To extract a string between two words in XML file

i need to extract the string between two tags, input file is <PersonInfoShipTo AddressID="446311709" AddressLine1="" AddressLine2="" AddressLine3="" AddressLine4="" AddressLine5="" AddressLine6="" AlternateEmailID="" Beeper="" City="" Company="" Country="" DayFaxNo="" DayPhone="" Department=""... (5 Replies)
Discussion started by: Padmanabhan
5 Replies

3. Shell Programming and Scripting

extract part of string using sed

Hi, I have the following string and I need to extract the date from it: TextHere,2012/07/11,1 I tried using sed: sed -n 's#^.*\({4}/{2}/{2}\).*$#\1#p' But it doesn't return anything. The following line doesn't even return '2012': sed -n 's/^.*\({4}\).*$/\1/p' I used to use grep -o... (6 Replies)
Discussion started by: Subbeh
6 Replies

4. Shell Programming and Scripting

Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string " This has been entered in MS_RECENT_ACTIVITY the... (2 Replies)
Discussion started by: simpletech369
2 Replies

5. Shell Programming and Scripting

Extract data b/w two words using sed

Hi, I want to extract data b/w two words with the help of sed.. Eg: In "Anna said that she would fetch the bucket", I want to display data b/w "Anna" and "would" O/P: said that she I have tried with below code, but unable to get desired o/p echo "Anna said that she would fetch the... (5 Replies)
Discussion started by: divya bandipotu
5 Replies

6. Shell Programming and Scripting

Sed filter words from string

I have following string in a variable: str="sstring garbage adfsdf tab.col1 lkad rjfj tab2.col2 adja tab2.col4 garbage" I want to filter "word.word" pattern from it. I assume the pattern won't occur in start or end of the string. Output shoud be: tab.col1 tab2.col2 tab2.col4 Can this be... (7 Replies)
Discussion started by: amicon007
7 Replies

7. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

8. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

9. Shell Programming and Scripting

using sed to conditionally extract stanzas of a file based on a search string

Dear All, I have a file with the syntax below (composed of several <log ..... </log> stanzas) I need to search this file for a number e.g. 2348022225919, and if it is found in a stanza, copy the whole stanza/section (<log .... </log>) to another output file. The numbers to search for are... (0 Replies)
Discussion started by: aitayemi
0 Replies

10. Shell Programming and Scripting

extract a string after a pattern using sed

I have a very large file and each line has a pattern and it is not position specific. I need to extract the string after the pattern ****MI* is the pattern in the red color 12 digit number is the sting value in the green color and it ends with ~ e.g. ... (1 Reply)
Discussion started by: gunaah
1 Replies
Login or Register to Ask a Question