Text File Pattern Mattching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text File Pattern Mattching
# 1  
Old 10-24-2006
Text File Pattern Mattching

If you have a text file where the word 'src:' appears in every line just different fields. What shell command is best to search for 'src:' and then print the next field. Same for 'dst:'


Text file would contain the following string on every line just at different fields locations.

'src: x.x.x.x; dst: x.x.x.x;'

In the past I was able to just use awk w/ print because the field was constant.

Thank you very much
# 2  
Old 10-24-2006
try sed

See if this works with little modification
Code:
sed -n 's/\(src:\)\(.*\)\(;\)\(dst:\)\(.*\)/\2  \5/p' filename

If you could post some testdata, it would be a great help.
# 3  
Old 10-24-2006
Data

Here is a line in one place

8:15:30 accept 10.1.5.252 >eth-s1/s1p1c0 rule: 165; rule_uid: {A67EC623-96B2-4606-9759-3BA602966892}; service_id: http; src: 0.0.0.0; dst: 0.0.0.0; proto: tcp; xlatedst: 10.3.132.26; NAT_rulenum: 305; NAT_addtnl_rulenum: 0; product: VPN-1 & FireWall-1; service: 80; s_port: 11945;

Here is a line in another place


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;
# 4  
Old 10-24-2006
a try - chk it

A little involved try - be very careful about each space
Code:
sed -f sedfile ipfilename  --where sedfile contains

s/^.*\(src: \)\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\);\( dst: \)\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\
);.*$/\1 \2 \3 \4/

See if it works.
# 5  
Old 10-24-2006
or using awk.

nawk -f barney.awk myFile.txt
nawk -v pat='dst:' -f barney.awk myFile.txt

barney.awk:
Code:
BEGIN {
  FS=";"
  # IP is a kind of lame regex, but.....
  IP="[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*"
  pat = (( pat == "")  ? "src: " : pat " ") IP
}

$0 ~ pat {
   for(i=1; i <=NF; i++)
      if ($i ~ pat)
         print $(i+1)
}

# 6  
Old 10-24-2006
Thank you Both

Both sed and awk worked in thoes formats. Saved 6 yrs of my life thank you thank you thank you
# 7  
Old 10-25-2006
one more question

How would I get service: bla also. I tried several 100 things since my last post but I am evidently and ID10T and cant seem to get it to pull service: bla in cordinance w/ src and dest.
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