[SOLVED] Filter through line/s to grab specific fields/data in the line with example
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Filter through line/s to grab specific fields/data in the line with example
Hi, I am having a problem of getting specific data from fields in a line for example in this line (there are multiple lines like this just to let you know)
'MOP22','auth','info','info','26','2009-10-02 15:13:11','snort','snort[5193]: [1:254:7] DNS SPOOF query response with TTL of 1 min. and no authority [Classification: Potentially Bad Traffic] [Priority: 2]: {UDP} 208.67.220.220:53 -> 95.224.96.106:50543',7174744
I want to get just the 2 IP addresses at the end i.e. 208.67.220.220 and 95.224.96.106 but using this command (extra problem is I need the port numbers beside at the end each IP address i.e 208.67.220.220:53
cat filename | awk -F ":" '{ print $9 }'
i only get
{UDP} 208.67.220.220
however this commands works when there is an extra field like in this line
In your first post you said you wanted port numbers, then you post output w/o them. Is this because you want them but that line was ICMP or because you don't want them?
To answer your question, Forrest there are no ports associated with ICMP, only TCP and UDP. I only selected ICMP as an example and in relation to the revised I might try to add a comma as a separator in order to separate them in spreadsheet.. Also, to PTrenholme I don't have GAWK installed on my machine, just MAWK and when I ran your code it was like an infinite loop so I don't know if the probklem was that the code only functions properly with GAWK or there was an error in the code.
I know there are no ports w/ ICMP, that's why I asked the question. Do you want the ports included if it is TCP or UDP (meaning that your example lacked them simply because it was an ICMP entry) or do you not want them for TCP and UDP as well (meaning your example lacked them because you don't want them at all)?
Any suggestions though for replacing the highlighted field below with a converted field?
'MOP22','auth','info','info','26','2009-10-02 15:13:11','snort','snort[5193]: [1:254:7] DNS SPOOF query response with TTL of 1 min. and no authority [Classification: Potentially Bad Traffic] [Priority: 2]: {UDP} 208.67.220.220:53 -> 95.224.96.106:50543',7174744
To be like this
'MOP22','auth','info','info','26','115688929','snort','snort[5193]: [1:254:7] DNS SPOOF query response with TTL of 1 min. and no authority [Classification: Potentially Bad Traffic] [Priority: 2]: {UDP} 208.67.220.220:53 -> 95.224.96.106:50543',7174744
. . . Also, to PTrenholme I don't have GAWK installed on my machine, just MAWK and when I ran your code it was like an infinite loop so I don't know if the probklem was that the code only functions properly with GAWK or there was an error in the code.
According to the MAWK manual, that code (which is POSIX-standard AWK) should have run.
Try this:
Code:
{
# Skip all fields preceding the 9th ":"-delimited field
FS=":"
$0=$0 # Re-parse the input line
if (NF < 9) next # Skip if there are fewer than 9 fields
for (i=9;i <=NF;++i) {
s=s (s==""?"":":") $i
}
sub(/[^0-9.,:]+/," ",s) # Replace any character not a digit, dot, colon, or comma by a blank
sub(/^ +/,"",s) # Remove any leading blanks
# Re-parse using blank as a field delimiter
FS=" "
$0=s
# Output the result
print "From line " NR ": " s
}
Run like this:
Code:
$ awk -f sep.awk sep.txt
From line 1: 208.67.220.220:53 -> 95.224.96.106:50543,7174744
Where that code is in sep.awk and the data file is in sep.txt. (Obviously, I have only your example line in my sep.txt.)
Note that this code:
Code:
BEGIN {
FS=":"
}
{
# Skip all fields preceding the 9th ":"-delimited field
if (NF < 9) next # Skip if there are fewer than 9 fields
for (i=9;i <=NF;++i) {
s=s (s==""?"":":") $i
}
sub(/[^0-9.,:]+/," ",s) # Replace any character not a digit, dot, colon, or comma by a blank
sub(/^ +/,"",s) # Remove any leading blanks
# Output the result
print "From line " NR ": " s
}
also works. I put the parse/re-parse and FS change stuff in the first code block to illustrate how you can dynamically change the field separator. You may find that technique useful in more sophisticated programs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.