简体   繁体   中英

extract data from text file with linux

I have file and I need to extract some data. the problem I'm facing is some line not almost the same with other lines. here is the example:

 action=accept trandisp=noop srcip=1.1.1.1 dstip=2.2.2.2 service=PING proto=1 duration=61 
 action=accept trandisp=noop srcip=1.1.1.1 dstip=3.3.3.3 dstport=80 service=http proto=1 duration=61

I want to get the destination IP with service in the first row, and the destination IP with dstport and service in the second line.

I'm new in linux and I tried it with grep and cut but it didn't work for me. please help me with the explanation of your answer.

What about this one?
grep -o -P "dstip=[0-9.]+ (dstport=[0-9]+)? service=\\w+ (dstport=[0-9]+)?" your-file

Explanation:

-o, --only-matching       show only the part of a line matching PATTERN
-P, --perl-regexp         PATTERN is a Perl regular expression

Of course, key-value order matters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM