tags:

views:

38

answers:

2

Hi, I am having a file that has the following lines say

13:26:35.655029 (TN) sh:sdf:sdf > ra:ram:raml, type Normal (800), length 21: ID 10.1.1.1 > 20.2.2.2: Addr 77: TP
13:26:35.656029 (TN) ra:ram:raml > sh:sdf:sdf, type Normal (800), length 21: ID 20.2.2.2 >10.1.1.1: Addr 77: TP 

I need to get sh:sdf:sdf , ra:ram:raml ,10.1.1.1 , 20.2.2.2 from 1st line and ra:ram:raml sh:sdf:sdf 20.2.2.2 10.1.1.1 from second line. how to do this using sed or awk

+1  A: 
awk -F"[>,)]" '{gsub(/.*ID /,"",$6);gsub(/Addr.*/,"",$7);print $2,$3,$6,$7 }' file

Basically, it set the field delimiters to 3 types of characters, >, , and ). then $2,$3,$6,$7 will contain what you want but with some extraneous strings. So we use gsub() to take care of them.

@OP, Sorry i couldn't explain more. Its best if you try it out on the command line. Start with the basics,

awk -F"[>,)]" '{print $2,$3,$6,$7 }' file

print each fields and see what happens. then insert back the gsub() statements one by one and see what happens.

ghostdog74
I think that the OP wants to compare two files and output the interline differences where they exist. This is just a simple extraction.
D.Shawley
I think you should read the question again.
ghostdog74
Hi.. It is working fine.. Thanks.. But can you please explain how u did this? I am not able to understand your solution. :(
shayam
oh.. tats kewl.. if possible could you please explain a bit more. or give some link where i can read about this. sorry to disturb u..:(
shayam
its always best to go the documentation: www.gnu.org/manual/gawk/gawk.html.
ghostdog74
A: 

I'm not sure if you can get differences at this level from sed or awk. You are probably better off using Perl, Python, or Ruby. There might be some awk wizardry that could handle the job since you could process one file as input and read a line from the other file every iteration using getline <filename varname. I/O on files other than the input stream is strange if you have never used it before, but you could probably get this approach to work.

Here's a start in a completely untested Awk script. It has been a while since I've written anything more than a one-liner in awk so this might not be entirely correct.

{
    left_line = $0
    getline <'other-file.txt' right_line
    left_count = split(left_line, left_tokens)
    right_count = split(right_line, right_tokens)
    if (left_count >= right_count)
       token_count = left_count
    else if (right_count > left_count)
       token_count = right_count
    for (token_index=0; token_index<token_count; token_index+=1) {
       if (left_tokens[token_index] != right_tokens[token_index]) {
          print left_tokens[token_index], right_tokens[token_index]
       }
    }
}
D.Shawley
that's not what he wants.
ghostdog74
Sorry ... I thought that by "using awk or sed to compare" he actually meant doing the comparison using awk or sed not simple extraction.
D.Shawley