Locating and Extracting Specific Patterns from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Locating and Extracting Specific Patterns from a file
# 1  
Old 04-28-2011
Locating and Extracting Specific Patterns from a file

Hi all,

1. I have a file that is getting continously refreshed (appended)
I want to grep all the strings containing substring of the type
Code:
abcdf123@aaa.xxx.yyy.zzz:portnumber:

where,

before @, any letters or numbers combination,
after @, IP address
then symbol :
then port number (any number)
then :

2. All these strings to be written in a filename in a format "username" "ip"

3. I already use tail -f to get the updates to this file (say x.txt)

Any ideas?
Thank you in advance.

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 04-28-2011 at 10:21 AM.. Reason: scottn: Post moved to separate thread - please do not Hijack threads! zaxxon: code tags
# 2  
Old 04-28-2011
What is the delimeter in front of the username? Or maybe post a complete example line using code tags, thanks.
# 3  
Old 04-28-2011
delimeter

before the username is a space.
after the :
there is a space too.
# 4  
Old 04-28-2011
I have the + escaped with \. You will have to try out if this is necessary with your sed too; I am on cygwin at the moment so I am not sure.

Code:
$ cat infile
aaa bbb abcdf123@aaa.xxx.yyy.zzz:portnumber: ccc ddd

zaxxon@saburac ~
$ sed 's/.* \([^@]\+\)@\([^:]\+\):.*/\1 \2/' infile > newfile

zaxxon@saburac ~
$ cat newfile
abcdf123 aaa.xxx.yyy.zzz

In your case you would use it like:
Code:
tail -f somelog | sed 's/.* \([^@]\+\)@\([^:]\+\):.*/\1 \2/' >> user_ip.out

# 5  
Old 04-29-2011
Locating and Extracting Specific Patterns from a file

Hi Zaxxon.

Thank you very much for your assitstance.

The sed part worked fine, it cathced the requested type strings exactly, from a given file.
The
Code:
 tail -f <reportFile> | sed ......      >>  <outputFile>

didn' produce any output though
Also is it possible to provide some logic in constructing the regular expression in the sed part? It seems quite magical for me!

Thank you again.

---------- Post updated at 03:46 PM ---------- Previous update was at 10:45 AM ----------

I found the problem about no writing to output file. The file had to be preexisting...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

2. UNIX for Advanced & Expert Users

Extracting specific lines from data file

Hello, Is there a quick awk one-liner for this extraction?: file1 49389 text55 52211 text66 file2 59302 text1 49389 text2 85939 text3 52211 text4 13948 text5 Desired output 49389 text2 52211 text4 Thanks!! (5 Replies)
Discussion started by: palex
5 Replies

3. Shell Programming and Scripting

Extracting content from a file in specific format

Hi All, I have the file in this format **** Results Data **** Time or Step 1 2 20 0.000000000e+00 0s 0s 0s 1.024000000e+00 Us 0s 0s 1.100000000e+00 1s 0s 0s 1.100000001e+00 1s 0s 1s 2.024000000e+00 Us Us 1s 2.024000001e+00 ... (7 Replies)
Discussion started by: diehard
7 Replies

4. Shell Programming and Scripting

Extracting a set of patterns from the text file

Hi experts, I need a help in extracting a set of patterns from the text file. Below is my scenario. Input file: I need to extract the data between My output should be as Thanks, Kalai (7 Replies)
Discussion started by: kalpeer
7 Replies

5. Shell Programming and Scripting

Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All, I have a small query. I have a file containing the following lines File 1: 29-Jul-2011 GMT Static data requires update <Extraction should start here> ----------- ----------- -------------------- ----------------------- ----------- <should stop here> Pattern1 will be time... (2 Replies)
Discussion started by: gangii87
2 Replies

6. UNIX for Dummies Questions & Answers

Extracting specific files from a tar file in HP-UX

I have tried: tar -xfv mytarfile.tar archive/tabv/* tar -xfv mytarfile.tar --wildcards 'archive/tabv/*' tar -xf mytarfile.tar -v --wildcards 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards --no-anchored 'archive/tabv/*' tar -xfv mytarfile.tar --wildcards `archive/tabv/*` and none... (5 Replies)
Discussion started by: zapper222
5 Replies

7. Shell Programming and Scripting

extracting specific lines from a file

hi all, i searched in unix.com and accquired the following commands for extracting specific lines from a file .. sed -n '16482,16482p' in.sql > out.sql awk 'NR>=10&&NR<=20' in.sql > out.sql.... these commands are working fine if i give the line numbers as such .. but if i pass a... (2 Replies)
Discussion started by: sais
2 Replies

8. Shell Programming and Scripting

shell script to format file based on specific patterns

Please help me out and drag me out the deadlock I am stuck into: I have a file. I want the statements under a if...then condition be listed in a separate file in the manner condition|statement.Following are the different input pattern and corresponding output parameters.any generic code to... (7 Replies)
Discussion started by: joyan321
7 Replies

9. Shell Programming and Scripting

Extracting specific text from a file

Dear All, I have to extract a a few lines from a log file and I know the starting String and end string(WHich is same ). Is there any simplere way using sed - awk. e.g. from the following file -------------------------------------- Some text Date: 21 Oct 2008 Text to be extracted... (8 Replies)
Discussion started by: rahulkav
8 Replies

10. UNIX for Dummies Questions & Answers

Is extracting specific files from a zip file possible?

If a zip file contains several zip files, but if the file names of the files needed are known, is there a variation of the unzip command that will allow those few (individual) files to be extracted? --- Example: Zip file name: zip.zip unzip -l zip.zip will display file01, file02, file03, etc.... (1 Reply)
Discussion started by: HLee1981
1 Replies
Login or Register to Ask a Question