Prev: Can a shell script determine if its output will be redirected?
Next: xgawk script for display xml tags and data
From: Janis Papanagnou on 28 Mar 2010 07:01 Hongyi Zhao wrote: > On Sat, 27 Mar 2010 12:49:03 +0100, Janis Papanagnou > <janis_papanagnou(a)hotmail.com> wrote: > >> I just noticed that the ISP_NAME can contain spaces, so the suggested >> solution wouldn't work. Sorry. To fix that you can re-define the FS in >> awk as " " (i.e. as three characters quote, space, quote), and remove >> the quotes from the search pattern if you're comparing field 2. > > What about just use the quote as field separator and use $2,$4,$6 to > catch the correponding three fields? That's an alternative approach. Janis > > $ echo '"121.44.240.31" "INTERNET SERVICE PROVIDER" "ON.NET"' | awk > -F'"' '{print $2,$4,$6 }' > 121.44.240.31 INTERNET SERVICE PROVIDER ON.NET
From: Ed Morton on 28 Mar 2010 11:36 On 3/27/2010 9:35 PM, Hongyi Zhao wrote: > On Sat, 27 Mar 2010 12:49:03 +0100, Janis Papanagnou > <janis_papanagnou(a)hotmail.com> wrote: > >> I just noticed that the ISP_NAME can contain spaces, so the suggested >> solution wouldn't work. Sorry. To fix that you can re-define the FS in >> awk as " " (i.e. as three characters quote, space, quote), and remove >> the quotes from the search pattern if you're comparing field 2. > > What about just use the quote as field separator and use $2,$4,$6 to > catch the correponding three fields? > > $ echo '"121.44.240.31" "INTERNET SERVICE PROVIDER" "ON.NET"' | awk > -F'"' '{print $2,$4,$6 }' > 121.44.240.31 INTERNET SERVICE PROVIDER ON.NET If you're only going to print the fields you really care about, just don't forget to also assign OFS before printing so you don't lose the quotes around your fields and print all your fields or otherwise provide spacing between those fields so you don't lose the spacing. Compare the following to the above: $ echo '"121.44.240.31" "INTERNET SERVICE PROVIDER" "ON.NET"' | awk 'BEGIN{FS=OFS="\""} {print $1,$2,$3,$4,$5,$6,$7 }' "121.44.240.31" "INTERNET SERVICE PROVIDER" "ON.NET" In reality you don't need to set OFS as you wont be recompiling or just printing selected fields from $0 so, the solution to your original problem would be doing two passes on the file (or read it into an array if you prefer): awk -F'"' -v tgtIp="151.48.43.174" ' NR==FNR { if ($2 == tgtIp) { tgtIsp=$4 tgtDom=$6 } next } ($4 != tgtIsp) && ($6 != tgtDom) && (($4 != "-") || ($6 != "-")) ' file file Regards, Ed.
From: Hongyi Zhao on 28 Mar 2010 22:00 On Sun, 28 Mar 2010 10:36:05 -0500, Ed Morton <mortonspam(a)gmail.com> wrote: >awk -F'"' -v tgtIp="151.48.43.174" ' >NR==FNR { > if ($2 == tgtIp) { > tgtIsp=$4 > tgtDom=$6 > } > next >} >($4 != tgtIsp) && ($6 != tgtDom) && (($4 != "-") || ($6 != "-")) >' file file Excellent solution. Thanks a lot. If I only want to obtain the IP_ADDRESS from the filtered results, e.g, for my original problem, the following should be the output: 117.18.75.235 121.44.240.31 122.155.3.145 140.109.17.180 145.100.100.190 149.9.0.57 149.9.0.58 149.9.0.59 151.15.8.46 151.21.86.208 151.23.7.196 How should your above-mentioned code be polished? BR. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Ed Morton on 28 Mar 2010 22:08 On 3/28/2010 9:00 PM, Hongyi Zhao wrote: > On Sun, 28 Mar 2010 10:36:05 -0500, Ed Morton<mortonspam(a)gmail.com> > wrote: > >> awk -F'"' -v tgtIp="151.48.43.174" ' >> NR==FNR { >> if ($2 == tgtIp) { >> tgtIsp=$4 >> tgtDom=$6 >> } >> next >> } >> ($4 != tgtIsp)&& ($6 != tgtDom)&& (($4 != "-") || ($6 != "-")) >> ' file file > > Excellent solution. Thanks a lot. > > If I only want to obtain the IP_ADDRESS from the filtered results, > e.g, for my original problem, the following should be the output: > > 117.18.75.235 > 121.44.240.31 > 122.155.3.145 > 140.109.17.180 > 145.100.100.190 > 149.9.0.57 > 149.9.0.58 > 149.9.0.59 > 151.15.8.46 > 151.21.86.208 > 151.23.7.196 > > How should your above-mentioned code be polished? > I don't mean to be rude but if you can't figure that out then there's really no point adopting that solution as you'll never be able to enhance it or do anything remotely similar in future. Why not try to figure it out and then just post back what you tried if it didn't work? Ed.
From: Hongyi Zhao on 28 Mar 2010 22:55 On Sun, 28 Mar 2010 21:08:49 -0500, Ed Morton <mortonspam(a)gmail.com> wrote: >I don't mean to be rude but if you can't figure that out then there's really no >point adopting that solution as you'll never be able to enhance it or do >anything remotely similar in future. > >Why not try to figure it out and then just post back what you tried if it didn't >work? Thank you for your meaningful recommendations:-) I've tried with your code. The results and what I want is described as follows: $ cat file "IP_ADDRESS" "ISP_NAME" "DOMAIN_NAME" "119.11.42.164" "-" "a.b.c" "151.16.191.218" "IUNET-BNET" "38-151.NET24.IT" "151.48.43.174" "-" "38-151.NET24.IT" "151.53.80.237" "IUNET-BNET" "38-151.NET24.IT" $ cat del_ip_records awk -F'"' -v tgtIp="151.48.43.174" ' NR==FNR { if ($2 == tgtIp) { tgtIsp=$4 tgtDom=$6 } next } (($4 != tgtIsp)||($4 != "-")) && ($6 != tgtDom) && (($4 != "-") || ($6 != "-")) ' file file In the above example, it's clearly that run your code will remove all of the records from the file: $ ./del_ip_records "IP_ADDRESS" "ISP_NAME" "DOMAIN_NAME" But I want the following result as the output: 119.11.42.164 If the record corresponding to the given IP has the following characteristics: one of these two fields, i.e. "ISP_NAME" or "DOMAIN_NAME" has the value: "-", do the following thing: 1- If the "ISP_NAME" of the record corresponding to the given IP has the value: "-", use the "DOMAIN_NAME" as the matching conditions to do the deteting operations. 2- If the "DOMAIN_NAME" of the record corresponding to the given IP has the value: "-", use the "ISP_NAME" as the matching conditions to do the deteting operations. 3- As the output, I only want obtain the IP_ADDRESS other than the entire records. In the above minimal example, the ISP_NAME of the given IP, 151.48.43.174, has the value "-", so we should use the "DOMAIN_NAME" of this IP as the matching conditions to do the deteting operations. Thanks for your further consideration. -- ..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Can a shell script determine if its output will be redirected? Next: xgawk script for display xml tags and data |