extract string portion using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract string portion using sed
# 1  
Old 05-20-2009
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/_[0-9].*[0-9]//'`

but I only get
TOS_TABIN218
TOS_TABIN219
TOS_TABIN219

The TOS TABIN218 is correct but both Tabin219 are missing the _1 and _2 respectively.
.. any how to get the extra _1 and _2

Thanks in advance

Satnam
# 2  
Old 05-20-2009
if you have Python
Code:
# python -c "for line in open('file'):print line[:line.index('_2009')]"
TOS_TABIN218
TOS_TABIN219_1
TOS_TABIN219_2

awk
Code:
# awk '{gsub(/_2009.*/,"")}1' file
TOS_TABIN218
TOS_TABIN219_1
TOS_TABIN219_2

# 3  
Old 05-20-2009
Code:
sed 's/_[0-9][0-9]*[.][0-9][0-9]*$//' myFile

# 4  
Old 05-20-2009
WIkid!... works a treat. should have mentioned I was using bash v 3.x. used the last suggetsion

's/_[0-9][0-9]*[.][0-9][0-9]*$//'

Cheers

Sat
# 5  
Old 05-20-2009
Another one with sed:

Code:
sed 's/\(.*\)_.*/\1/' file

# 6  
Old 05-22-2009
hi man i think this will help u
TOS_TABIN218_20090323.200903231830
TOS_TABIN219_1_20090323.200903231830
TOS_TABIN219_2_20090323.200903231830

x=TOS_TABIN218_20090323.200903231830
xx=TABIN219_2_20090323.200903231830

echo ${x%_*}
TOS_TABIN218
echo ${xx%_*}
TABIN219_2

or
sed 's/_2009.*//' file

Last edited by maxim42; 05-22-2009 at 04:55 PM..
# 7  
Old 05-22-2009
Quote:
Originally Posted by santam
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/_[0-9].*[0-9]//'`

Don't use external commands to manipulate strings.

Code:
source_tabin_name=${fname%_*}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract a portion of string from each line in Linux

Hi I have to extract the destination path information from each record the file is of variable length so I will not be able to use the print command.The search should start on variable "destinationPath" and it should end at immediate "," also the first field has to be printed Input File:... (7 Replies)
Discussion started by: rkakitapalli
7 Replies

2. 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

3. Shell Programming and Scripting

Extract portion of data

Hi Gurus, I need some help in extracting some of these information and massage it into the desired output as shown below. I need to extract the last row with the header in below sample which is usually the most recent date, for example: 2012-06-01 142356 mb 519 -219406 mb 1 ... (9 Replies)
Discussion started by: superHonda123
9 Replies

4. Shell Programming and Scripting

How to extract portion of a string?

Hi Gurus, Would like to seek some help on how to extract a portion of string from log's output as shown below. Sample of raw data: piece handle=/test123/disk_dump/test123/df0_cntrl_PCPFCI20120404_68498 tag=TAG20120404T180035 comment=NONE piece... (13 Replies)
Discussion started by: superHonda123
13 Replies

5. 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

6. Shell Programming and Scripting

[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... (7 Replies)
Discussion started by: dips_ag
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 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

9. Shell Programming and Scripting

extract a portion of log file

hello, I want to grep the log file according to time and get the portion of log from one particular time to other. I can grep for individual lines by time but how should I print lines continuously from given start time till end till given end time. Appreciate your ideas, Thanks chandra (8 Replies)
Discussion started by: chandra004
8 Replies

10. UNIX for Dummies Questions & Answers

How to extract a portion of a string from the whole string

How to extract a portion of a string from a full string using unix. For example: Say source string is = "req92374923.log" I want only the numeric portion of the string say "92374923" how to do that in Unix. (2 Replies)
Discussion started by: ds_sastry
2 Replies
Login or Register to Ask a Question