extract a string within a string using a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract a string within a string using a pattern
# 1  
Old 11-04-2010
extract a string within a string using a pattern

hi all,

i have a file name using the following pattern:

PREFIX: AR
SOURCE: LEGACY
DATETIME: YYYYMMDD_HH24MISS
SUFFIX: .txt

sample filename:

AR_LEGACY_20101104_105500.txt

i want to extract the source which is LEGACY in this case. how do i do this using shell?

thanks.
# 2  
Old 11-05-2010
You didn't mention what shell. If you use the external sed command this should work for most:

Code:
SOURCE=`echo $file | sed "s/^AR_//;s/_.*//"`

For bash/ksh
Code:
SOURCE=${file#AR_}
SOURCE=${SOURCE%%_*}

# 3  
Old 11-05-2010
Quote:
Originally Posted by Chubler_XL
You didn't mention what shell. If you use the external sed command this should work for most:
Code:
SOURCE=`echo $file | sed "s/^AR_//;s/_.*//"`

thanks. how can i get the shell ? i'm using Linux.

Code:
[ ~]$ uname
Linux

how can i do this in sed even if prefix is other than AR? i tried the following but doesnt work.
Code:
[ ~]$ source=`echo $file | sed "s/^*_//;s/_.*//"`
[ ~]$ echo $source
AR
[ ~]$ source=`echo $file | sed "s/*_//;s/_.*//"`
[ ~]$ echo $source
AR

Quote:
Originally Posted by Chubler_XL
For bash/ksh
Code:
SOURCE=${file#AR_}
SOURCE=${SOURCE%%_*}

thanks.. actually the prefix can be other than AR. so i tried
Code:
file=AR_LEGACY_20101104_105500.txt
source=${file#*_}
source=${source%%_*}
echo $source

seems to work.
# 4  
Old 11-05-2010
To determine shell:

Code:
$ ps -p$$

If other than AR_ this should work too:

Code:
SOURCE=`echo $file | sed "s/^[^_]*_//;s/_.*//"`

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-05-2010
[~]$ ps -p$$
PID TTY TIME CMD
20855 pts/5 00:00:00 bash
[~]$

thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to extract every repeated string between two specific string?

Hello guys, I have problem with hpux shell script. I have one big text file that contains like SOH bla bla bla bla bla bla ETX SOH bla bla bla ETX SOH bla bla bla ETX What I need to do is save first SOH*BLA into file1.txt, save second SOH*BLA into file2.txt and so on.... (17 Replies)
Discussion started by: sembii
17 Replies

2. Shell Programming and Scripting

To Search for a string and to extract the string from the text

Hi Team I have an huge xml where i need to search for a ceratin numbers. For example 2014-05-06 15:15:41,498 INFO WebContainer : 10 CommonServicesLogs - CleansingTriggerService.invokeCleansingService Entered PUBSUB NOTIFY MESSAGE () - <?xml version="1.0" encoding="UTF-8"... (5 Replies)
Discussion started by: Kannannair
5 Replies

3. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

4. Shell Programming and Scripting

Pattern Match & Extract from a string

Hi, I have long string in 2nd field, as shown below: REF1 | CLESCLJSCSHSCSMSCSNSCSRSCUDSCUFSCU7SCV1SCWPSCXGPDBACAPA0DHDPDMESED6 REF2 | SBR4PCBFPCDRSCSCG3SCHEBSCKNSCKPSCLLSCMCZXTNPCVFPCV6P4KL0DMDSDSASEWG I have a group of fixed patterns which can occur in these long strings & only... (11 Replies)
Discussion started by: karumudi7
11 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

7. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

8. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 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