Text File Pattern Mattching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text File Pattern Mattching
# 8  
Old 10-25-2006
Try...
Code:
awk -v tags='(service:|src:|dst:)' '{
    while(match($0,"[^a-z]" tags "[^;]*;")){
        printf substr($0,RSTART+1,RLENGTH)
        $0=substr($0,RSTART+RLENGTH)}
    printf ORS}' file1

...gives...
Code:
src: 0.0.0.0; dst: 0.0.0.0; service: 80;
src: 0.0.0.0; dst: 0.0.0.0; service: tcp-8000-30000;

# 9  
Old 10-25-2006
Python alternative:

Code:
#!/usr/bin/python
import re #reg expression module
s = """14:38:51 accept 30.146.28.4 >eth2c0 src: 0.0.0.0; dst: 0.0.0.0; proto: tcp; xlatedst: 10.85.92.65; NAT_rulenum: 50; NAT_addtnl_rulenum: 0; rule: 30; product: VPN-1 & FireWall-1; service: tcp-8000-30000; s_port: 55479;"""

s_ip = re.findall(" src: (.*?);",s)
d_ip = re.findall(" dst: (.*?);",s)
service = re.findall(" service: (.*?);",s)

print "Source IP: " , s_ip[0]
print "Destination IP: ",d_ip[0]
print "Service: ", service[0]

output:

Code:
jupiter:/home/#  ./test.py
Source IP:  0.0.0.0
Destination IP:  0.0.0.0
Service:  tcp-8000-30000

# 10  
Old 10-25-2006
awsome

Thank you all very much simply awsome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename text file with a specific pattern in directory

I am trying to rename all text files in a directory that match a pattern. The current command below seems to be using the directory path in the name and since it already exists, will not do the rename. I am not sure what I am missing? Thank you :). Files to rename in... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

3. Shell Programming and Scripting

To Search for a pattern and substring text in a file

I have the following data in a text file. "A",1,"MyTextfile.CSV","200","This is ,line one" "B","EFG",23,"MyTextfile1.csv","5621",562,"This is ,line two" I want to extract the fileNames MyTextfile.CSV and MyTextfile1.csv. The problem is not all the lines are delimited with "," There are... (3 Replies)
Discussion started by: AshTrak
3 Replies

4. Shell Programming and Scripting

Insert rows of text into a file after pattern

SHELL=bash OS=rhel I have a file1 that contains text in sentences like so: file1 this is a sentence this is a sentence this is a sentence I also have a file2 like so: file2 command=" here is some text here is more text again we have some text " I wish to echo the text from... (4 Replies)
Discussion started by: jaysunn
4 Replies

5. Shell Programming and Scripting

Help with remove last text of a file that have specific pattern

Input file matrix-remodelling_associated_8_ aurora_interacting_1_ L20 von_factor_A_domain_1 ATP_containing_3B_ . . Output file matrix-remodelling_associated_8 aurora_interacting_1 L20 von_factor_A_domain_1 ATP_containing_3B . . (3 Replies)
Discussion started by: perl_beginner
3 Replies

6. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

7. Shell Programming and Scripting

extract unique pattern from large text file

Hi All, I am trying to extract data from a large text file , I want to extract lines which contains a five digit number followed by a hyphen , like 12345- , i tried with egrep ,eg : egrep "+" text.txt but which returns all the lines which contains any number of digits followed by hyhen ,... (19 Replies)
Discussion started by: shijujoe
19 Replies

8. AIX

Pattern to replace ^M and ^Y in a 4.2 AIX text file

I have files on my AIX 4.2 client system where I need to do the following replacements below but have no clue how ? They are control characters (linefeed, chariage return, ...). First, replace "^M^Y^M" with ^char_for_end_of_line Then replace "^M" with " " Trim all left spaces In VI, my... (7 Replies)
Discussion started by: Browser_ice
7 Replies

9. Shell Programming and Scripting

insert text into another file after matching pattern

i am not sure what i should be using but would like a simple command that is able to insert a certain block of text that i define or from another text file into a xml file after a certain match is done for e.g insert the text </servlet-mapping> <!-- beechac added - for epic post-->... (3 Replies)
Discussion started by: cookie23patel
3 Replies

10. UNIX for Advanced & Expert Users

extraction of data from a text file which follows certain pattern

hi everybody, i have a file, in it I need to extract some data that follows a particular pattern.. For example: my file contains like now running Speak225 sep 22 mon 16:34:05 2008 -------------------------------- ... (4 Replies)
Discussion started by: mohkris
4 Replies
Login or Register to Ask a Question