How to capture a string enclose by a pattern within a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to capture a string enclose by a pattern within a file?
# 1  
Old 10-25-2013
How to capture a string enclose by a pattern within a file?

Hi all,

My file :test.txt just like this:

...........................
Code:
From: 333:123<sip:88888888888@bbbb.com
To: <sip:123456@aaaaa.com

.........................

I want a script to capture the string between sip: & @

Expect output:
Code:
88888888888
123456


Please help!

Last edited by Don Cragun; 10-25-2013 at 12:38 AM.. Reason: Add CODE tags.
# 2  
Old 10-25-2013
Try something like:
Code:
sed 's/.*sip://;s/@.*//' test.txt

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-25-2013
In awk

Code:
$ awk 'gsub(".*sip:|@.*",x)' file

88888888888
123456

In Perl

Code:
$ perl -pe 's/.*sip:|@.*//g' file

88888888888
123456

Using Grep

Code:
$ grep -Po '(?<=sip:).*(?=@)' file

88888888888
123456

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 10-27-2013
Great!
Many thanks for Don & Akshay.
# 5  
Old 10-28-2013
Some more awk
awk -F"sip:|@" '{print $2}' file

gnu awk
awk '{print gensub(/.*sip:(.*)@.*/,"\\1","g")}' file
This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Enclose String in single quote

I have a comma separated file which contains data like; File header: ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3 File Detail: AMR,5100,Total Services,305,D0120,,, AMR,5101,Periodic Services,40702,H2011,U1,, AMR,5112,Day... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

2. Shell Programming and Scripting

Script to capture string in a log file

Dear all, I have a log file to be analysed. this log file contains vaiours lines of code starting with date timestamp. if my search string is exception then that resepective log statement starting from the date is required. example: 2014/10/01 16:14:44.459|>=|E|X|19202496|2832|... (5 Replies)
Discussion started by: shravee
5 Replies

3. Shell Programming and Scripting

String capture from ip file

Dear All From below mention input file I want op file as mention. Kindly help. IP file: "BSCGNR4_IPA17_C" 329 140119 0717 RXOCF-105 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Needed OP: 140119 0717 KJO001_BASC_NG AC FAULTY DG ON DOOR OPEN Note that string mark in red as variable in... (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 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. UNIX for Dummies Questions & Answers

Capture filenames with a Pattern into another file

Hi i have a command in windows which captures the filenames with the same pattern and route it to another file. for example filetest_20110921, filetest_20110922,filetest_20110923 etc I would like to know the command in UNIX/LINUX for the same. The command which i am using in Windows is ... (12 Replies)
Discussion started by: mansurkhalil
12 Replies

7. Shell Programming and Scripting

need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and... (3 Replies)
Discussion started by: iskatel
3 Replies

8. Shell Programming and Scripting

Need to capture certain text from a string in a different file

Hi, I wanted to know how i could accomplish this in a script using ksh. Lets say there is a file called test.dat and it has a certain input like below : . . Hi = 56 Hi = 67 . . 1 record(s) selected Now i need to capture the numbers after the = sign and store them in a... (3 Replies)
Discussion started by: Siddarth
3 Replies

9. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

10. UNIX for Advanced & Expert Users

pattern match in each line and capture it question

I need a clarification on one of the scripts that i have written, I new to file handling and i need help: I am trying to find a particular pattern in a file "****** PBX" in set of 5 files named: xy.cc3 xv.cc3 xx.cc3 xr.cc3 xd.cc3 in a directory. If i find the files starting with these... (16 Replies)
Discussion started by: bsandeep_80
16 Replies
Login or Register to Ask a Question