开发者

using awk or sed to compare

开发者 https://www.devze.com 2023-01-13 02:58 出处:网络
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

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


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.


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]
       }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号